Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Fix piston break speed
Browse files Browse the repository at this point in the history
  • Loading branch information
LlewVallis committed Aug 20, 2020
1 parent 7147f48 commit 99e5848
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion artifacts/paper/url
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions artifacts/protocollib/url
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/dmulloy2/ProtocolLib/releases/download/4.5.1/ProtocolLib.jar
2 changes: 1 addition & 1 deletion artifacts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..
Expand Down
11 changes: 11 additions & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.5.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.github.Querz</groupId>
<artifactId>NBT</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +26,7 @@
@ApiVersion(ApiVersion.Target.v1_15)
@Author("Llew Vallis <llewvallis@gmail.com>")
@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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Player, BlockBreakStatus> 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();
}
}
1 change: 1 addition & 0 deletions server/plugins/protocollib.jar

0 comments on commit 99e5848

Please sign in to comment.