diff --git a/.gradle/8.8/checksums/checksums.lock b/.gradle/8.8/checksums/checksums.lock index 474482e..5a2ef67 100644 Binary files a/.gradle/8.8/checksums/checksums.lock and b/.gradle/8.8/checksums/checksums.lock differ diff --git a/.gradle/8.8/checksums/md5-checksums.bin b/.gradle/8.8/checksums/md5-checksums.bin index 06b02ce..3b4c601 100644 Binary files a/.gradle/8.8/checksums/md5-checksums.bin and b/.gradle/8.8/checksums/md5-checksums.bin differ diff --git a/.gradle/8.8/checksums/sha1-checksums.bin b/.gradle/8.8/checksums/sha1-checksums.bin index 97c2d4c..f0b03fb 100644 Binary files a/.gradle/8.8/checksums/sha1-checksums.bin and b/.gradle/8.8/checksums/sha1-checksums.bin differ diff --git a/.gradle/8.8/executionHistory/executionHistory.bin b/.gradle/8.8/executionHistory/executionHistory.bin index 86584f8..aea8c67 100644 Binary files a/.gradle/8.8/executionHistory/executionHistory.bin and b/.gradle/8.8/executionHistory/executionHistory.bin differ diff --git a/.gradle/8.8/executionHistory/executionHistory.lock b/.gradle/8.8/executionHistory/executionHistory.lock index 259c97b..ca17322 100644 Binary files a/.gradle/8.8/executionHistory/executionHistory.lock and b/.gradle/8.8/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.8/fileHashes/fileHashes.bin b/.gradle/8.8/fileHashes/fileHashes.bin index f01632f..212c1b7 100644 Binary files a/.gradle/8.8/fileHashes/fileHashes.bin and b/.gradle/8.8/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.8/fileHashes/fileHashes.lock b/.gradle/8.8/fileHashes/fileHashes.lock index 7b59e48..b5576cd 100644 Binary files a/.gradle/8.8/fileHashes/fileHashes.lock and b/.gradle/8.8/fileHashes/fileHashes.lock differ diff --git a/.gradle/8.8/fileHashes/resourceHashesCache.bin b/.gradle/8.8/fileHashes/resourceHashesCache.bin index 88f21d4..71705d1 100644 Binary files a/.gradle/8.8/fileHashes/resourceHashesCache.bin and b/.gradle/8.8/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index f299812..c08a949 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties index 6b90fa7..34becd8 100644 --- a/.gradle/buildOutputCleanup/cache.properties +++ b/.gradle/buildOutputCleanup/cache.properties @@ -1,2 +1,2 @@ -#Thu Jun 27 17:21:31 SGT 2024 +#Sun Aug 04 10:18:49 SGT 2024 gradle.version=8.8 diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index 3bc02c7..14c175b 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index fca7167..7126711 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/src/main/java/mikeshafter/mikestcaddons/Commands.java b/src/main/java/mikeshafter/mikestcaddons/Commands.java index 8e7bc2c..2104970 100644 --- a/src/main/java/mikeshafter/mikestcaddons/Commands.java +++ b/src/main/java/mikeshafter/mikestcaddons/Commands.java @@ -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; @@ -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; @@ -42,8 +45,21 @@ public void enable (MikesTCAddons plugin) { @Command("throttle ") @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 ") @@ -63,13 +79,17 @@ public void swapCmd (final CommandSender sender, final MikesTCAddons plugin, fin @Command("changeitem ") @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(); } } @@ -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 { diff --git a/src/main/java/mikeshafter/mikestcaddons/MikesTCAddons.java b/src/main/java/mikeshafter/mikestcaddons/MikesTCAddons.java index 197b91d..d99f72d 100644 --- a/src/main/java/mikeshafter/mikestcaddons/MikesTCAddons.java +++ b/src/main/java/mikeshafter/mikestcaddons/MikesTCAddons.java @@ -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)); @@ -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!"); } diff --git a/src/main/java/mikeshafter/mikestcaddons/attachments/Changer.java b/src/main/java/mikeshafter/mikestcaddons/attachments/Changer.java index cc69823..7499d83 100644 --- a/src/main/java/mikeshafter/mikestcaddons/attachments/Changer.java +++ b/src/main/java/mikeshafter/mikestcaddons/attachments/Changer.java @@ -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 { @@ -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; } } diff --git a/src/main/java/mikeshafter/mikestcaddons/attachments/RecurseHelper.java b/src/main/java/mikeshafter/mikestcaddons/attachments/RecurseHelper.java index 734630e..1d1e77a 100644 --- a/src/main/java/mikeshafter/mikestcaddons/attachments/RecurseHelper.java +++ b/src/main/java/mikeshafter/mikestcaddons/attachments/RecurseHelper.java @@ -2,8 +2,6 @@ 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; @@ -11,27 +9,27 @@ public abstract class RecurseHelper { 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 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 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); } diff --git a/src/main/java/mikeshafter/mikestcaddons/dynamics/PSDListener.java b/src/main/java/mikeshafter/mikestcaddons/dynamics/PSDListener.java index f86093a..71ef278 100644 --- a/src/main/java/mikeshafter/mikestcaddons/dynamics/PSDListener.java +++ b/src/main/java/mikeshafter/mikestcaddons/dynamics/PSDListener.java @@ -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 { @@ -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 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); -// } -//} - } diff --git a/src/main/java/mikeshafter/mikestcaddons/dynamics/SignActionPSD.java b/src/main/java/mikeshafter/mikestcaddons/dynamics/SignActionPSD.java index b8788d0..6d3a66a 100644 --- a/src/main/java/mikeshafter/mikestcaddons/dynamics/SignActionPSD.java +++ b/src/main/java/mikeshafter/mikestcaddons/dynamics/SignActionPSD.java @@ -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; @@ -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); } } @@ -96,8 +97,9 @@ private List getSigns (Block block, int dx, int dy, int dz) { // check if the signs are correct for (Sign sign : signs) { List 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); } } diff --git a/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHApproach.java b/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHApproach.java index d678ed5..3423021 100644 --- a/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHApproach.java +++ b/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHApproach.java @@ -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; @@ -42,7 +39,6 @@ public class SignActionRHApproach extends SignAction { FileConfiguration config = new FileConfiguration(plugin, "config.yml"); var sequence = config.getNode("announcement-format."+operator); - return; } } diff --git a/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHProperty.java b/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHProperty.java index 7272d70..3669910 100644 --- a/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHProperty.java +++ b/src/main/java/mikeshafter/mikestcaddons/signactions/SignActionRHProperty.java @@ -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"); } @@ -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()); } } diff --git a/src/main/java/mikeshafter/mikestcaddons/throttle/ManGear.java b/src/main/java/mikeshafter/mikestcaddons/throttle/ManGear.java new file mode 100644 index 0000000..a097fb9 --- /dev/null +++ b/src/main/java/mikeshafter/mikestcaddons/throttle/ManGear.java @@ -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); +} + +} diff --git a/src/main/java/mikeshafter/mikestcaddons/throttle/SimpleLever.java b/src/main/java/mikeshafter/mikestcaddons/throttle/SimpleLever.java new file mode 100644 index 0000000..5ef25f7 --- /dev/null +++ b/src/main/java/mikeshafter/mikestcaddons/throttle/SimpleLever.java @@ -0,0 +1,49 @@ +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 SimpleLever implements Throttle { + +private final MinecartGroup controlledGroup; +private double speed; +private float gear = 0.0004f; +private float current; + +public SimpleLever (MinecartGroup controlledGroup) { + this.controlledGroup = controlledGroup; +} + +@Override +public void onReceive (int bitArray) { + // SimpleLever only uses A/D buttons to control the lever. + // We get the 8's bit for A + if ((bitArray & 8) != 0) { + this.speed = this.controlledGroup.getAverageForce() - 0.002; + // P = 0.5 mva + // I = P/V + current = (float) (speed * 0.002 / gear); + if (current >= 0.8f) gear += 0.0002f; + this.controlledGroup.setForwardForce(speed); + } + // We get the 16's bit for D + if ((bitArray & 16) != 0) { + this.speed = this.controlledGroup.getAverageForce() + 0.002; + // P = 0.5 mva + // I = P/V + current = (float) (speed * 0.002 / gear); + if (current <= 0.2f && gear > 0.0004f) gear -= 0.0002f; + this.controlledGroup.setForwardForce(speed); + } +} + +@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); +} + +} diff --git a/src/main/java/mikeshafter/mikestcaddons/throttle/Throttle.java b/src/main/java/mikeshafter/mikestcaddons/throttle/Throttle.java new file mode 100644 index 0000000..d6c1ca7 --- /dev/null +++ b/src/main/java/mikeshafter/mikestcaddons/throttle/Throttle.java @@ -0,0 +1,9 @@ +package mikeshafter.mikestcaddons.throttle; + +import org.bukkit.entity.Player; + +public interface Throttle { + +void onReceive (int bitArray); +void updateHUD (Player player); +} diff --git a/src/main/java/mikeshafter/mikestcaddons/throttle/ThrottleController.java b/src/main/java/mikeshafter/mikestcaddons/throttle/ThrottleController.java new file mode 100644 index 0000000..7fbe781 --- /dev/null +++ b/src/main/java/mikeshafter/mikestcaddons/throttle/ThrottleController.java @@ -0,0 +1,68 @@ +package mikeshafter.mikestcaddons.throttle; +import com.bergerkiller.bukkit.common.events.PacketReceiveEvent; +import com.bergerkiller.bukkit.common.events.PacketSendEvent; +import com.bergerkiller.bukkit.common.protocol.PacketListener; +import com.bergerkiller.bukkit.common.protocol.PacketType; +import mikeshafter.mikestcaddons.MikesTCAddons; +import org.bukkit.entity.Player; + +public class ThrottleController implements PacketListener { + +private static final MikesTCAddons plugin = MikesTCAddons.getPlugin(MikesTCAddons.class); +private static final Player[] p = new Player[plugin.getServer().getMaxPlayers()]; +private static final Throttle[] t = new Throttle[plugin.getServer().getMaxPlayers()]; + +public static void addThrottle (Player player, Throttle throttle) { + for (int i = 0; i < p.length; i++) { + if (p[i] == null) { + p[i] = player; + t[i] = throttle; + break; + } + } +} + +public static void removeThrottle (Player player) { + for (int i = 0; i < p.length; i++) { + if (p[i] == player) { + p[i] = null; + t[i] = null; + break; + } + } +} + +@Override +public void onPacketReceive (PacketReceiveEvent event) { + Player player = event.getPlayer(); + if (getThrottle(player) == null) return; + + final var throttle = getThrottle(player); + if (event.getType() != PacketType.IN_STEER_VEHICLE) return; + float forwards = event.getPacket().read(PacketType.IN_STEER_VEHICLE.forwards); + float sideways = event.getPacket().read(PacketType.IN_STEER_VEHICLE.sideways); + boolean jump = event.getPacket().read(PacketType.IN_STEER_VEHICLE.jump); + + int bits = 0; + // jump: 1, forwards: 2, backwards: 4, left: 8, right: 16 + bits |= jump ? 1 : 0; + bits |= forwards > 0 ? 2 : 0; + bits |= forwards < 0 ? 4 : 0; + bits |= sideways > 0 ? 8 : 0; + bits |= sideways < 0 ? 16 : 0; + + throttle.onReceive(bits); + throttle.updateHUD(player); +} + +public Throttle getThrottle (Player player) { + for (int i = 0; i < p.length; i++) { + if (p[i] == player) return t[i]; + } + return null; +} + +@Override +public void onPacketSend (PacketSendEvent event) { +} +} diff --git a/src/main/java/mikeshafter/mikestcaddons/util/AddonsUtil.java b/src/main/java/mikeshafter/mikestcaddons/util/AddonsUtil.java index 9fde225..e738e61 100644 --- a/src/main/java/mikeshafter/mikestcaddons/util/AddonsUtil.java +++ b/src/main/java/mikeshafter/mikestcaddons/util/AddonsUtil.java @@ -21,7 +21,6 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.intellij.lang.annotations.Subst; - import java.util.*; @@ -94,20 +93,14 @@ public static void announcePoly (String message, int[]... points) { } } -private static boolean inPolygonWE (int x, int y, int z, int[][] vertices) { - boolean result = java.util.stream.IntStream.range(0, vertices.length) - .map(i -> - (vertices[i][2] > z) != (vertices[(i + 1) % vertices.length][2] > z) ? - (x < (vertices[(i + 1) % vertices.length][0] - vertices[i][0]) * (z - vertices[i][2]) / (vertices[(i + 1) % vertices.length][2] - vertices[i][2]) + vertices[i][0]) ? - 1 : 0 - : 0).sum() % 2 == 1; - - int minY = vertices[0][1], maxY = vertices[0][1]; - for (int j = 1; j < vertices.length; j++) { - minY = Math.min(minY, vertices[j][1]); - maxY = Math.max(maxY, vertices[j][1]); +// Play sound in a sphere shape +public static void playSoundSphere (double x, double y, double z, double r, String sound, String source, float volume, float pitch) { + for (Player player : Bukkit.getOnlinePlayers()) { + Location location = player.getLocation(); + if ((location.getX() - x) * (location.getX() - x) + (location.getY() - y) * (location.getY() - y) + (location.getZ() - z) * (location.getZ() - z) <= r * r) { + _playSound(player, sound, source, volume, pitch); + } } - return minY <= y && y <= maxY && result; } // Sets a certain SignLink variable @@ -160,16 +153,6 @@ public static void playSoundCuboid(double x1, double y1, double z1, double x2, d } } -// Play sound in a sphere shape -public static void playSoundSphere(double x, double y, double z, double r, String sound, String source, float volume, float pitch) { - for (Player player : Bukkit.getOnlinePlayers()) { - Location location = player.getLocation(); - if ((location.getX() - x) * (location.getX() - x) + (location.getY() - y) * (location.getY() - y) + (location.getZ() - z) * (location.getZ() - z) <= r * r) { - _playSound(player, sound, source, volume, pitch); - } - } -} - public static void playSoundPoly (String sound, String source, float volume, float pitch, int[]... points) { for (Player player : Bukkit.getOnlinePlayers()) { Location location = player.getLocation(); @@ -180,6 +163,17 @@ public static void playSoundPoly (String sound, String source, float volume, flo } } +private static boolean inPolygonWE (int x, int y, int z, int[][] vertices) { + boolean result = java.util.stream.IntStream.range(0, vertices.length).map(i -> (vertices[i][2] > z) != (vertices[(i + 1) % vertices.length][2] > z) ? (x < (vertices[(i + 1) % vertices.length][0] - vertices[i][0]) * (z - vertices[i][2]) / (vertices[(i + 1) % vertices.length][2] - vertices[i][2]) + vertices[i][0]) ? 1 : 0 : 0).sum() % 2 == 1; + + int minY = vertices[0][1], maxY = vertices[0][1]; + for (int j = 1; j < vertices.length; j++) { + minY = Math.min(minY, vertices[j][1]); + maxY = Math.max(maxY, vertices[j][1]); + } + return minY <= y && y <= maxY && result; +} + public static void openDoor(World world, int x, int y, int z, BlockFace direction, long openTime) { Location location = new Location(world, x, y, z); openDoor(location, direction, openTime); @@ -191,14 +185,16 @@ public static void openDoor (Location loc, BlockFace direction, long openTime) { // optimise code if (!(block.getType() == Material.AIR || block.getType() == Material.CAVE_AIR || block.getType() == Material.VOID_AIR) && (block.getBlockData() instanceof Fence || block.getBlockData() instanceof GlassPane) && !loc.getWorld().getNearbyEntities(loc, 48, 32, 48, (entity) -> entity.getType() == EntityType.PLAYER).isEmpty()) { PlatformGate platformGate = new PlatformGate(block, direction, openTime); - platformGate.activateGate(); gates.put(loc, platformGate); + platformGate.activateGate(); + gates.put(loc, platformGate); } } public static void closeDoor (Location loc) { if (gates.get(loc) != null) { MikesTCAddons.getPlugin(MikesTCAddons.class).getLogger().info("Closing gate " + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ()); - gates.get(loc).closeGate(false); gates.remove(loc); + gates.get(loc).closeGate(false); + gates.remove(loc); } } @@ -228,9 +224,7 @@ public static int parseRelative (String str, char axis, Location loc) { * @return The block to reference */ public static Block getReferenceBlock (org.bukkit.block.Sign sign) { - if (sign.getBlockData() instanceof org.bukkit.block.data.type.WallSign) { - return BlockUtil.getAttachedBlock((Block) sign); - } return sign.getBlock(); + return sign.getBlockData() instanceof org.bukkit.block.data.type.WallSign ? BlockUtil.getAttachedBlock((Block) sign) : sign.getBlock(); } /** @@ -240,11 +234,7 @@ public static Block getReferenceBlock (org.bukkit.block.Sign sign) { * @return Returns the rotation of a standing sign, and the facing of a wall sign. */ public static BlockFace getSignFacing (org.bukkit.block.Sign sign) { - if (sign.getBlockData() instanceof org.bukkit.block.data.type.WallSign w) { - return w.getFacing(); - } else if (sign.getBlockData() instanceof org.bukkit.block.data.type.Sign s) {return s.getRotation();} else { - return BlockFace.SELF; - } + return sign.getBlockData() instanceof org.bukkit.block.data.type.WallSign w ? w.getFacing() : sign.getBlockData() instanceof org.bukkit.block.data.type.Sign s ? s.getRotation() : BlockFace.SELF; } /** @@ -264,8 +254,6 @@ public static List parseComponents (List cList) { * @return the Component in String format */ public static String parseComponent (final Component c) { - if (c instanceof TextComponent) {return ((TextComponent) c).content();} else if (c == null) {return "";} else { - return c.examinableName(); - } + return c instanceof TextComponent ? ((TextComponent) c).content() : c == null ? "" : c.examinableName(); } } diff --git a/src/main/resources/sequences.yml b/src/main/resources/sequences.yml new file mode 100644 index 0000000..e69de29