This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7147f48
commit 99e5848
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
plugin/src/main/java/org/astropeci/omw/listeners/PistonBreakHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../artifacts/protocollib/bin |