From 99e5848ab89b13df9e889e7e7f1cc4b6ca0fe78e Mon Sep 17 00:00:00 2001 From: LlewVallis Date: Thu, 20 Aug 2020 17:51:43 +1000 Subject: [PATCH] Fix piston break speed --- artifacts/paper/url | 2 +- artifacts/protocollib/url | 1 + artifacts/setup.sh | 2 +- plugin/pom.xml | 11 ++ .../astropeci/omw/OpenMissileWarsPlugin.java | 7 ++ .../omw/listeners/PistonBreakHandler.java | 118 ++++++++++++++++++ server/plugins/protocollib.jar | 1 + 7 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 artifacts/protocollib/url create mode 100644 plugin/src/main/java/org/astropeci/omw/listeners/PistonBreakHandler.java create mode 120000 server/plugins/protocollib.jar diff --git a/artifacts/paper/url b/artifacts/paper/url index 440c493..66feab7 100644 --- a/artifacts/paper/url +++ b/artifacts/paper/url @@ -1 +1 @@ -https://papermc.io/ci/job/Paper-1.16/28/artifact/paperclip-28.jar +https://papermc.io/ci/job/Paper-1.16/136/artifact/paperclip-136.jar diff --git a/artifacts/protocollib/url b/artifacts/protocollib/url new file mode 100644 index 0000000..de51861 --- /dev/null +++ b/artifacts/protocollib/url @@ -0,0 +1 @@ +https://github.com/dmulloy2/ProtocolLib/releases/download/4.5.1/ProtocolLib.jar \ No newline at end of file diff --git a/artifacts/setup.sh b/artifacts/setup.sh index 137c050..a879332 100755 --- a/artifacts/setup.sh +++ b/artifacts/setup.sh @@ -12,7 +12,7 @@ for dir in */; do echo No cache for "$ARTIFACT_URL", downloading fi - curl --output bin "$ARTIFACT_URL" && echo "$ARTIFACT_URL" > url-cached + curl -L --output bin "$ARTIFACT_URL" && echo "$ARTIFACT_URL" > url-cached fi cd .. diff --git a/plugin/pom.xml b/plugin/pom.xml index b608184..b5f7aca 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -59,9 +59,20 @@ jitpack.io https://jitpack.io + + dmulloy2-repo + https://repo.dmulloy2.net/nexus/repository/public/ + + + com.comphenix.protocol + ProtocolLib + 4.5.1 + provided + + com.github.Querz NBT diff --git a/plugin/src/main/java/org/astropeci/omw/OpenMissileWarsPlugin.java b/plugin/src/main/java/org/astropeci/omw/OpenMissileWarsPlugin.java index d64291f..4eaaf0f 100644 --- a/plugin/src/main/java/org/astropeci/omw/OpenMissileWarsPlugin.java +++ b/plugin/src/main/java/org/astropeci/omw/OpenMissileWarsPlugin.java @@ -11,6 +11,8 @@ import org.bukkit.event.Listener; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.plugin.java.annotation.dependency.Dependency; +import org.bukkit.plugin.java.annotation.dependency.DependsOn; import org.bukkit.plugin.java.annotation.permission.Permission; import org.bukkit.plugin.java.annotation.permission.Permissions; import org.bukkit.plugin.java.annotation.plugin.ApiVersion; @@ -24,6 +26,7 @@ @ApiVersion(ApiVersion.Target.v1_15) @Author("Llew Vallis ") @Website("https://github.com/LlewVallis/OpenMissileWars") +@DependsOn(@Dependency("ProtocolLib")) @Permissions({ @Permission(name = "omw.arena.join", defaultValue = PermissionDefault.TRUE), @Permission(name = "omw.github", defaultValue = PermissionDefault.TRUE), @@ -63,6 +66,7 @@ public void onEnable() { EquipmentProvider equipmentProvider = new EquipmentProvider(); NightVisionHandler nightVisionHandler = new NightVisionHandler(); + PistonBreakHandler pistonBreakHandler = new PistonBreakHandler(this); new HubCommand(hub).register(this); new TemplateCommand(template).register(this); @@ -92,6 +96,9 @@ public void onEnable() { registerEventHandler(new PortalBreakListener(arenaPool, this)); registerEventHandler(new ExplosionModifier()); registerEventHandler(nightVisionHandler); + registerEventHandler(pistonBreakHandler); + + pistonBreakHandler.register(); } private void registerEventHandler(Listener listener) { diff --git a/plugin/src/main/java/org/astropeci/omw/listeners/PistonBreakHandler.java b/plugin/src/main/java/org/astropeci/omw/listeners/PistonBreakHandler.java new file mode 100644 index 0000000..e9f1b11 --- /dev/null +++ b/plugin/src/main/java/org/astropeci/omw/listeners/PistonBreakHandler.java @@ -0,0 +1,118 @@ +package org.astropeci.omw.listeners; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketEvent; +import com.destroystokyo.paper.event.server.ServerTickStartEvent; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +@RequiredArgsConstructor +public class PistonBreakHandler implements Listener { + + private final Plugin plugin; + + private final Object lock = new Object(); + + private final Map playerBreakingStatuses = new HashMap<>(); + private final AtomicInteger currentTick = new AtomicInteger(); + + private static final int MAXIMUM_BREAKING_PAUSE = 1; + private static final int BREAK_SPEED = 20; + + @AllArgsConstructor + private static class BlockBreakStatus { + + public Block block; + public int lastTickClicked; + public int ticksOfProgress; + } + + private class PacketListener extends PacketAdapter { + + public PacketListener() { + super(PistonBreakHandler.this.plugin, PacketType.Play.Client.ARM_ANIMATION); + } + + @Override + public void onPacketReceiving(PacketEvent e) { + Player player = e.getPlayer(); + synchronized (lock) { + onArmSwing(player); + } + } + + private void onArmSwing(Player player) { + Block targetedBlock = getTargetedBlock(player); + int currentTick = PistonBreakHandler.this.currentTick.get(); + + BlockBreakStatus status = playerBreakingStatuses.get(player); + if (status == null) { + if (targetedBlock != null) { + BlockBreakStatus newStatus = new BlockBreakStatus(targetedBlock, currentTick, 1); + playerBreakingStatuses.put(player, newStatus); + } + + return; + } + + long timeSinceLastAnimation = currentTick - status.lastTickClicked; + status.lastTickClicked = currentTick; + + if (targetedBlock == null) { + status.ticksOfProgress = 0; + return; + } + + if (!targetedBlock.equals(status.block)) { + BlockBreakStatus newStatus = new BlockBreakStatus(targetedBlock, currentTick, 1); + playerBreakingStatuses.put(player, newStatus); + return; + } + + if (timeSinceLastAnimation > MAXIMUM_BREAKING_PAUSE + 1) { + status.ticksOfProgress = 0; + return; + } + + status.ticksOfProgress += timeSinceLastAnimation; + + Material material = targetedBlock.getType(); + boolean isPiston = material == Material.PISTON || material == Material.STICKY_PISTON || material == Material.PISTON_HEAD; + + if (status.ticksOfProgress >= BREAK_SPEED && isPiston) { + playerBreakingStatuses.remove(player); + + targetedBlock.getWorld().playSound(targetedBlock.getLocation(), Sound.BLOCK_STONE_BREAK, 1, 0.8f); + Bukkit.getScheduler().runTask(plugin, (Runnable) targetedBlock::breakNaturally); + } + } + + private Block getTargetedBlock(Player player) { + return player.getTargetBlockExact(5); + } + } + + public void register() { + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListener()); + Bukkit.getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + private void onTick(ServerTickStartEvent e) { + currentTick.incrementAndGet(); + } +} diff --git a/server/plugins/protocollib.jar b/server/plugins/protocollib.jar new file mode 120000 index 0000000..9611185 --- /dev/null +++ b/server/plugins/protocollib.jar @@ -0,0 +1 @@ +../../artifacts/protocollib/bin \ No newline at end of file