Skip to content

Commit

Permalink
add throttle (untested)
Browse files Browse the repository at this point in the history
Signed-off-by: Mineshafter61 <yuhang.061@gmail.com>
  • Loading branch information
Mineshafter61 committed Aug 9, 2024
1 parent 17efce8 commit 1765ff5
Show file tree
Hide file tree
Showing 26 changed files with 264 additions and 141 deletions.
Binary file modified .gradle/8.8/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/8.8/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/8.8/checksums/sha1-checksums.bin
Binary file not shown.
Binary file modified .gradle/8.8/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.8/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu Jun 27 17:21:31 SGT 2024
#Sun Aug 04 10:18:49 SGT 2024
gradle.version=8.8
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
34 changes: 27 additions & 7 deletions src/main/java/mikeshafter/mikestcaddons/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import com.bergerkiller.bukkit.tc.properties.TrainProperties;
import mikeshafter.mikestcaddons.attachments.Changer;
import mikeshafter.mikestcaddons.attachments.Swapper;
import mikeshafter.mikestcaddons.throttle.ManGear;
import mikeshafter.mikestcaddons.throttle.SimpleLever;
import mikeshafter.mikestcaddons.throttle.Throttle;
import mikeshafter.mikestcaddons.throttle.ThrottleController;
import mikeshafter.mikestcaddons.util.AddonsUtil;
import org.bukkit.Material;
import org.bukkit.World;
Expand All @@ -20,7 +24,6 @@
import org.incendo.cloud.annotations.CommandDescription;
import org.incendo.cloud.annotations.Permission;
import org.incendo.cloud.annotations.processing.CommandContainer;

import java.util.Collections;
import java.util.List;

Expand All @@ -42,8 +45,21 @@ public void enable (MikesTCAddons plugin) {
@Command("throttle <type>")
@CommandDescription("Turns on or off throttle")
@Permission("mikestcaddons.throttle")
public void throttleCmd (final CommandSender sender, final MikesTCAddons plugin, final @Argument("type") String throttleType) {
// Command method here
public void throttleCmd (final CommandSender sender, final MikesTCAddons plugin, final @Argument("type") Integer throttleType) {
if (!(sender instanceof Player player)) return;
MinecartGroup vehicle = CartPropertiesStore.getEditing(player).getHolder().getGroup();
if (vehicle == null || !vehicle.getProperties().hasOwnership(player)) return;

if (throttleType == 0) {
ThrottleController.removeThrottle(player);
return;
}
Throttle throttle = switch (throttleType) {
case 1 -> new SimpleLever(vehicle);
case 2 -> new ManGear(vehicle);
default -> null;
};
if (throttle != null) ThrottleController.addThrottle(player, throttle);
}

@Command("swap <animation0> <animation1>")
Expand All @@ -63,13 +79,17 @@ public void swapCmd (final CommandSender sender, final MikesTCAddons plugin, fin
@Command("changeitem <name> <item_type> <custom_model_data>")
@CommandDescription("Changes an item on the train to the item specified")
@Permission("mikestcaddons.changeitem")
public void changeItemCmd (final CommandSender sender, final MikesTCAddons plugin, final @Argument("name") String name, final @Argument("item_type") String material, final @Argument("custom_model_data") Integer customModelData) {
public void changeItemCmd (final CommandSender sender, final MikesTCAddons plugin, final @Argument("name") String name, final @Argument("item_type") String matString, final @Argument("custom_model_data") Integer customModelData) {
if (!(sender instanceof Player player)) return;
MinecartGroup vehicle = CartPropertiesStore.getEditing(player).getHolder().getGroup();
if (vehicle == null || !vehicle.getProperties().hasOwnership(player)) return;

// material checker
if (Material.matchMaterial(matString.toUpperCase()) == null) return;
Material material = Material.matchMaterial(matString.toUpperCase());

for (MinecartMember<?> member : vehicle) {
Changer a = new Changer(member, name, Material.getMaterial(material.toUpperCase()), customModelData);
Changer a = new Changer(member, name, material, customModelData);
a.run();
}
}
Expand All @@ -93,10 +113,10 @@ public void decoupleCmd (final CommandSender sender, final MikesTCAddons plugin,
if (number < 0) {
number = -number;

for (int i = size - 1, j = 0; i > size - number; i--, j++) {
for (int i = size - 1, j = 0; i >= size - number; i--, j++) {
newGroup[j] = members.get(i);
vehicle.remove(i);
}
vehicle.subList(size - number, size).clear();
}

else {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/mikeshafter/mikestcaddons/MikesTCAddons.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package mikeshafter.mikestcaddons;

import com.bergerkiller.bukkit.common.protocol.PacketType;
import com.bergerkiller.bukkit.common.utils.PacketUtil;
import com.bergerkiller.bukkit.tc.signactions.SignAction;
import mikeshafter.mikestcaddons.attachments.SignActionAttachment;
import mikeshafter.mikestcaddons.attachments.SignActionSwap;
import mikeshafter.mikestcaddons.dynamics.PSDListener;
import mikeshafter.mikestcaddons.dynamics.SignActionPSD;
import mikeshafter.mikestcaddons.throttle.ThrottleController;
import mikeshafter.mikestcaddons.util.AddonsUtil;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Level;

public final class MikesTCAddons extends JavaPlugin {

private final SignActionSwap signActionSwap = new SignActionSwap();
private final SignActionPSD signActionPSD = new SignActionPSD();
private final SignActionAttachment signActionAttachment = new SignActionAttachment();
private final SignActionPSD signActionPSD = new SignActionPSD();
private final SignActionAttachment signActionAttachment = new SignActionAttachment();

@Override
public void onDisable() {
// Plugin shutdown logic
SignAction.unregister(signActionSwap); SignAction.unregister(signActionPSD);
SignAction.unregister(signActionSwap);
SignAction.unregister(signActionPSD);
SignAction.unregister(signActionAttachment);

AddonsUtil.gates.forEach((location, gate) -> gate.closeGate(true));
Expand All @@ -35,7 +38,9 @@ public void onEnable() {

SignAction.register(signActionSwap);
SignAction.register(signActionAttachment);
SignAction.register(signActionPSD); getServer().getPluginManager().registerEvents(new PSDListener(), this);
SignAction.register(signActionPSD);
getServer().getPluginManager().registerEvents(new PSDListener(), this);
PacketUtil.addPacketListener(this, new ThrottleController(), PacketType.IN_STEER_VEHICLE);

this.getLogger().log(Level.INFO, "Mike's TC Addons has been enabled!");
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/mikeshafter/mikestcaddons/attachments/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.bergerkiller.bukkit.tc.controller.MinecartMember;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;

public class Changer extends RecurseHelper {
Expand All @@ -26,12 +26,13 @@ protected ConfigurationNode call (ConfigurationNode node) {
if (!names.contains(this.name)) return node;

ItemStack item = node.get("item", ItemStack.class);
var meta = item.getItemMeta();
ItemMeta meta = item.getItemMeta();
item.setType(this.type);
meta.setCustomModelData(this.data);
item.setItemMeta(meta);
if (meta != null && this.data != 0) {
meta.setCustomModelData(this.data);
item.setItemMeta(meta);
}
node.set("item", item);

return node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@
import com.bergerkiller.bukkit.common.config.ConfigurationNode;
import com.bergerkiller.bukkit.tc.controller.MinecartMember;

import java.util.Set;

public abstract class RecurseHelper {

final MinecartMember<?> member;

public RecurseHelper (MinecartMember<?> member) {this.member = member;}

public void run () {
ConfigurationNode fullConfig = this.member.getProperties().getModel().getConfig();
var fullConfig = this.member.getProperties().getModel().getConfig();

ConfigurationNode attachmentsNode = fullConfig.getNode("attachments");
Set<ConfigurationNode> attachmentsSet = attachmentsNode.getNodes();
var attachmentsNode = fullConfig.getNode("attachments");
var attachmentsSet = attachmentsNode.getNodes();

if (attachmentsSet != null) {
for (ConfigurationNode innerNode : attachmentsSet) {
for (var innerNode : attachmentsSet) {
attachmentsNode.set(innerNode.getPath(), recurse(innerNode));
fullConfig.set("attachments", attachmentsNode);
}
}

this.member.getProperties().getModel().update(fullConfig);
this.member.getProperties().getModel().sync();
}

private ConfigurationNode recurse (ConfigurationNode node) {
ConfigurationNode attachmentsNode = node.getNode("attachments");
Set<ConfigurationNode> attachmentsSet = attachmentsNode.getNodes();
var attachmentsNode = node.getNode("attachments");
var attachmentsSet = attachmentsNode.getNodes();

if (attachmentsSet != null) {
for (ConfigurationNode innerNode : attachmentsSet) {
for (var innerNode : attachmentsSet) {
attachmentsNode.set(innerNode.getPath(), recurse(innerNode));
node.set("attachments", attachmentsNode);
}
Expand Down
67 changes: 0 additions & 67 deletions src/main/java/mikeshafter/mikestcaddons/dynamics/PSDListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
//import com.bergerkiller.bukkit.tc.Direction;
//import com.bergerkiller.bukkit.tc.PowerState;
//import mikeshafter.mikestcaddons.MikesTCAddons;
//import mikeshafter.mikestcaddons.util.AddonsUtil;
//import org.bukkit.Bukkit;
//import org.bukkit.Location;
//import org.bukkit.block.Sign;
//import org.bukkit.block.data.Lightable;
//import org.bukkit.block.data.type.WallSign;
//import org.bukkit.block.sign.Side;
//import org.bukkit.event.block.BlockPhysicsEvent;
//import java.util.List;

public class PSDListener implements Listener {

Expand All @@ -32,59 +20,4 @@ private String parseComponent (final Component c) {
if (c instanceof TextComponent) {return ((TextComponent) c).content();} else if (c == null) {return "";} else return c.examinableName();
}

//@EventHandler
//public void onBlockPhysics (BlockPhysicsEvent event) {
// if (!(event.getBlock().getBlockData() instanceof WallSign && event.getBlock().getBlockData() instanceof org.bukkit.block.data.type.Sign && event.getBlock().getState() instanceof Sign sign)) {
// return;
// }
//
// List<String> lines = AddonsUtil.parseComponents(sign.getSide(Side.FRONT).lines());
// if (!lines.get(0).equalsIgnoreCase("[psd]")) return;
//
// boolean powerState = PowerState.isSignPowered(event.getBlock());
// var loc = AddonsUtil.getReferenceBlock(sign).getLocation();
//
// if (powerState) {
// long openTicks = AddonsUtil.parseTicks(lines.get(2));
// var dir = Direction.parse(lines.get(3)).getDirection(AddonsUtil.getSignFacing(sign));
// for (int i = 1; i <= 5; i++) {
// loc.add(0d, 1d, 0d);
// // open for 5 minutes if no time is set
// AddonsUtil.openDoor(loc, dir, openTicks > 0 ? openTicks : 6000);
// if (openTicks > 0) {flashLamp(loc, openTicks);} else flashLamp(loc, true);
// }
// } else {
// for (int i = 1; i <= 5; i++) {
// loc.add(0d, 1d, 0d); AddonsUtil.closeDoor(loc); flashLamp(loc, false);
// }
// }
//}
//
//private void flashLamp (Location location, long openTicks) {
// if (location.getBlock().getState().getBlockData() instanceof Lightable lightable) {
// Runnable flashRun = () -> lightable.setLit(!lightable.isLit());
//
// // opening
// var open = Bukkit.getScheduler().runTaskTimer(MikesTCAddons.getPlugin(MikesTCAddons.class), flashRun, 0, 5);
// Bukkit.getScheduler().runTaskLater(MikesTCAddons.getPlugin(MikesTCAddons.class), () -> {
// open.cancel(); lightable.setLit(true);
// }, 45);
//
// // closing
// var close = Bukkit.getScheduler().runTaskTimer(MikesTCAddons.getPlugin(MikesTCAddons.class), flashRun, openTicks, 5);
// Bukkit.getScheduler().runTaskLater(MikesTCAddons.getPlugin(MikesTCAddons.class), () -> {
// close.cancel(); lightable.setLit(false);
// }, openTicks + 45);
// }
// }
//
//private void flashLamp (Location location, boolean endOn) {
// if (location.getBlock().getState().getBlockData() instanceof Lightable lightable) {
// var flashTask = Bukkit.getScheduler().runTaskTimer(MikesTCAddons.getPlugin(MikesTCAddons.class), () -> lightable.setLit(!lightable.isLit()), 0, 5);
// Bukkit.getScheduler().runTaskLater(MikesTCAddons.getPlugin(MikesTCAddons.class), () -> {
// flashTask.cancel(); lightable.setLit(endOn);
// }, 45);
// }
//}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.bukkit.block.Sign;
import org.bukkit.block.data.Lightable;
import org.bukkit.block.sign.Side;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -48,10 +47,12 @@ public void execute (SignActionEvent info) {
dx = ParseUtil.parseInt(parts[0], dx);
dy = ParseUtil.parseInt(parts[1], dy);
dz = ParseUtil.parseInt(parts[2], dz);
} else if (parts.length == 2) {
}
else if (parts.length == 2) {
dx = dz = ParseUtil.parseInt(parts[0], dx);
dy = ParseUtil.parseInt(parts[1], dy);
} else if (parts.length == 1) {
}
else if (parts.length == 1) {
dx = dy = dz = ParseUtil.parseInt(parts[0], dx);
}
}
Expand Down Expand Up @@ -96,8 +97,9 @@ private List<Sign> getSigns (Block block, int dx, int dy, int dz) {
// check if the signs are correct
for (Sign sign : signs) {
List<String> lines = AddonsUtil.parseComponents(sign.getSide(Side.FRONT).lines());
// incorrect, update!
if (!(lines.get(0).equalsIgnoreCase("[psd]"))) {
// goto front and return
updateSignCache = true;
return getSigns(block, dx, dy, dz);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.bergerkiller.bukkit.common.config.ConfigurationNode;
import com.bergerkiller.bukkit.common.config.FileConfiguration;
import com.bergerkiller.bukkit.common.utils.ParseUtil;
import com.bergerkiller.bukkit.tc.Direction;
import com.bergerkiller.bukkit.tc.attachments.animation.AnimationOptions;
import com.bergerkiller.bukkit.tc.events.SignActionEvent;
import com.bergerkiller.bukkit.tc.events.SignChangeActionEvent;
import com.bergerkiller.bukkit.tc.signactions.SignAction;
Expand Down Expand Up @@ -42,7 +39,6 @@ public class SignActionRHApproach extends SignAction {
FileConfiguration config = new FileConfiguration(plugin, "config.yml");
var sequence = config.getNode("announcement-format."+operator);

return;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bergerkiller.bukkit.tc.events.SignChangeActionEvent;
import com.bergerkiller.bukkit.tc.signactions.SignAction;
import com.bergerkiller.bukkit.tc.signactions.SignActionType;
import com.bergerkiller.bukkit.tc.utils.SignBuildOptions;

public class SignActionRHProperty extends SignAction {
@Override public boolean match (SignActionEvent info) { return info.isType("rhproperty"); }
Expand All @@ -20,6 +21,6 @@ public class SignActionRHProperty extends SignAction {
}

@Override public boolean build (SignChangeActionEvent event) {
return false;
return SignBuildOptions.create().setName("extra properties").setDescription("sets properties of trains").handle(event.getPlayer());
}
}
53 changes: 53 additions & 0 deletions src/main/java/mikeshafter/mikestcaddons/throttle/ManGear.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mikeshafter.mikestcaddons.throttle;

import com.bergerkiller.bukkit.tc.controller.MinecartGroup;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;

public class ManGear implements Throttle {

private final MinecartGroup controlledGroup;
private double speed;
private float gear = 0.0004f;
private float current;

public ManGear (MinecartGroup controlledGroup) {
this.controlledGroup = controlledGroup;
}

@Override
public void onReceive (int bitArray) {
// SimpleLever only uses A/D buttons to control the lever.
// We get the 2's bit for W
if ((bitArray & 2) != 0) {
this.speed = this.controlledGroup.getAverageForce() + 0.002;
// P = 0.5 mva
// I = P/V
current = (float) (speed * 0.002 / gear);
this.controlledGroup.setForwardForce(speed);
}
// We get the 16's bit for S
if ((bitArray & 4) != 0) {
this.speed = this.controlledGroup.getAverageForce() - 0.002;
// P = 0.5 mva
// I = P/V
current = (float) (speed * 0.002 / gear);
this.controlledGroup.setForwardForce(speed);
}
if ((bitArray & 8) != 0) {
gear += 0.0002f;
}
if ((bitArray & 16) != 0) {
if (gear > 0.0004f) gear -= 0.0002f;
}
}

@Override
public void updateHUD (Player player) {
player.sendActionBar(Component.text(speed));
BossBar.bossBar(Component.text("Main Reservoir").asComponent(), 0.75F, BossBar.Color.PINK, BossBar.Overlay.NOTCHED_10);
BossBar.bossBar(Component.text("Traction Current").asComponent(), current, BossBar.Color.RED, BossBar.Overlay.NOTCHED_10);
}

}
Loading

0 comments on commit 1765ff5

Please sign in to comment.