Cleanup & remove unnecessary calls in PlayerDiggingListener

This commit is contained in:
themode 2021-01-21 16:53:20 +01:00
parent 7a4f3672de
commit 4bb27c305f

View File

@ -17,6 +17,7 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket;
import net.minestom.server.potion.Potion; import net.minestom.server.potion.Potion;
import net.minestom.server.potion.PotionEffect; import net.minestom.server.potion.PotionEffect;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -29,114 +30,120 @@ public class PlayerDiggingListener {
final ClientPlayerDiggingPacket.Status status = packet.status; final ClientPlayerDiggingPacket.Status status = packet.status;
final BlockPosition blockPosition = packet.blockPosition; final BlockPosition blockPosition = packet.blockPosition;
final PlayerInventory playerInventory = player.getInventory();
final ItemStack mainHand = playerInventory.getItemInMainHand();
final ItemStack offHand = playerInventory.getItemInOffHand();
final Instance instance = player.getInstance(); final Instance instance = player.getInstance();
if (instance == null) if (instance == null)
return; return;
final short blockStateId = instance.getBlockStateId(blockPosition); if (status == ClientPlayerDiggingPacket.Status.STARTED_DIGGING) {
switch (status) { final short blockStateId = instance.getBlockStateId(blockPosition);
case STARTED_DIGGING: final boolean instantBreak = player.isCreative() ||
final boolean instantBreak = player.isCreative() || player.isInstantBreak() ||
player.isInstantBreak() || Block.fromStateId(blockStateId).breaksInstantaneously();
Block.fromStateId(blockStateId).breaksInstantaneously();
if (instantBreak) { if (instantBreak) {
// No need to check custom block // No need to check custom block
breakBlock(instance, player, blockPosition, blockStateId); breakBlock(instance, player, blockPosition, blockStateId);
} else { } else {
final CustomBlock customBlock = instance.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
final int customBlockId = customBlock == null ? 0 : customBlock.getCustomBlockId();
PlayerStartDiggingEvent playerStartDiggingEvent = new PlayerStartDiggingEvent(player, blockPosition, blockStateId, customBlockId);
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
if (playerStartDiggingEvent.isCancelled()) {
addEffect(player);
// Unsuccessful digging
sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
} else if (customBlock != null) {
// Start digging the custom block
if (customBlock.enableCustomBreakDelay()) {
customBlock.startDigging(instance, blockPosition, player);
addEffect(player);
}
sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
}
}
break;
case CANCELLED_DIGGING:
// Remove custom block target
player.resetTargetBlock();
sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING, true);
break;
case FINISHED_DIGGING:
final CustomBlock customBlock = instance.getCustomBlock(blockPosition); final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
if (customBlock != null && customBlock.enableCustomBreakDelay()) { final int customBlockId = customBlock == null ? 0 : customBlock.getCustomBlockId();
// Is not supposed to happen, probably a bug
PlayerStartDiggingEvent playerStartDiggingEvent = new PlayerStartDiggingEvent(player, blockPosition, blockStateId, customBlockId);
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
if (playerStartDiggingEvent.isCancelled()) {
addEffect(player);
// Unsuccessful digging
sendAcknowledgePacket(player, blockPosition, blockStateId, sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, false); ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
} else { } else if (customBlock != null) {
// Vanilla block // Start digging the custom block
breakBlock(instance, player, blockPosition, blockStateId); if (customBlock.enableCustomBreakDelay()) {
customBlock.startDigging(instance, blockPosition, player);
addEffect(player);
}
sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
} }
break; }
case DROP_ITEM_STACK:
final ItemStack droppedItemStack = player.getInventory().getItemInMainHand();
dropItem(player, droppedItemStack, ItemStack.getAirItem());
break;
case DROP_ITEM:
final int dropAmount = 1;
ItemStack handItem = player.getInventory().getItemInMainHand(); } else if (status == ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING) {
final StackingRule stackingRule = handItem.getStackingRule();
final int handAmount = stackingRule.getAmount(handItem);
if (handAmount <= dropAmount) { final short blockStateId = instance.getBlockStateId(blockPosition);
// Drop the whole item without copy // Remove custom block target
dropItem(player, handItem, ItemStack.getAirItem()); player.resetTargetBlock();
} else {
// Drop a single item, need a copy
ItemStack droppedItemStack2 = handItem.clone();
droppedItemStack2 = stackingRule.apply(droppedItemStack2, dropAmount); sendAcknowledgePacket(player, blockPosition, blockStateId,
ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING, true);
handItem = handItem.clone(); // Force the copy } else if (status == ClientPlayerDiggingPacket.Status.FINISHED_DIGGING) {
handItem = stackingRule.apply(handItem, handAmount - dropAmount);
dropItem(player, droppedItemStack2, handItem); final short blockStateId = instance.getBlockStateId(blockPosition);
} final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
break; if (customBlock != null && customBlock.enableCustomBreakDelay()) {
case UPDATE_ITEM_STATE: // Is not supposed to happen, probably a bug
player.refreshEating(false); sendAcknowledgePacket(player, blockPosition, blockStateId,
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false); ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, false);
} else {
// Vanilla block
breakBlock(instance, player, blockPosition, blockStateId);
}
if (itemUpdateStateEvent == null) { } else if (status == ClientPlayerDiggingPacket.Status.DROP_ITEM_STACK) {
player.refreshActiveHand(true, false, false);
} else { final ItemStack droppedItemStack = player.getInventory().getItemInMainHand();
final boolean isOffHand = itemUpdateStateEvent.getHand() == Player.Hand.OFF; dropItem(player, droppedItemStack, ItemStack.getAirItem());
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffHand, false);
} } else if (status == ClientPlayerDiggingPacket.Status.DROP_ITEM) {
final int dropAmount = 1;
ItemStack handItem = player.getInventory().getItemInMainHand();
final StackingRule stackingRule = handItem.getStackingRule();
final int handAmount = stackingRule.getAmount(handItem);
if (handAmount <= dropAmount) {
// Drop the whole item without copy
dropItem(player, handItem, ItemStack.getAirItem());
} else {
// Drop a single item, need a copy
ItemStack droppedItemStack2 = handItem.clone();
droppedItemStack2 = stackingRule.apply(droppedItemStack2, dropAmount);
handItem = handItem.clone(); // Force the copy
handItem = stackingRule.apply(handItem, handAmount - dropAmount);
dropItem(player, droppedItemStack2, handItem);
}
} else if (status == ClientPlayerDiggingPacket.Status.UPDATE_ITEM_STATE) {
player.refreshEating(false);
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false);
if (itemUpdateStateEvent == null) {
player.refreshActiveHand(true, false, false);
} else {
final boolean isOffHand = itemUpdateStateEvent.getHand() == Player.Hand.OFF;
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffHand, false);
}
} else if (status == ClientPlayerDiggingPacket.Status.SWAP_ITEM_HAND) {
final PlayerInventory playerInventory = player.getInventory();
final ItemStack mainHand = playerInventory.getItemInMainHand();
final ItemStack offHand = playerInventory.getItemInOffHand();
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(player, offHand, mainHand);
player.callCancellableEvent(PlayerSwapItemEvent.class, swapItemEvent, () -> {
playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem());
playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem());
});
break;
case SWAP_ITEM_HAND:
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(player, offHand, mainHand);
player.callCancellableEvent(PlayerSwapItemEvent.class, swapItemEvent, () -> {
playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem());
playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem());
});
break;
} }
} }
@ -168,7 +175,8 @@ public class PlayerDiggingListener {
} }
} }
private static void dropItem(Player player, ItemStack droppedItem, ItemStack handItem) { private static void dropItem(@NotNull Player player,
@NotNull ItemStack droppedItem, @NotNull ItemStack handItem) {
final PlayerInventory playerInventory = player.getInventory(); final PlayerInventory playerInventory = player.getInventory();
if (player.dropItem(droppedItem)) { if (player.dropItem(droppedItem)) {
playerInventory.setItemInMainHand(handItem); playerInventory.setItemInMainHand(handItem);
@ -185,7 +193,7 @@ public class PlayerDiggingListener {
* *
* @param player the player to add the effect to * @param player the player to add the effect to
*/ */
private static void addEffect(Player player) { private static void addEffect(@NotNull Player player) {
playersEffect.add(player); playersEffect.add(player);
EntityEffectPacket entityEffectPacket = new EntityEffectPacket(); EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
@ -208,7 +216,7 @@ public class PlayerDiggingListener {
* *
* @param player the player to remove the effect to * @param player the player to remove the effect to
*/ */
public static void removeEffect(Player player) { public static void removeEffect(@NotNull Player player) {
if (playersEffect.contains(player)) { if (playersEffect.contains(player)) {
playersEffect.remove(player); playersEffect.remove(player);
@ -228,8 +236,8 @@ public class PlayerDiggingListener {
* @param status the status of the digging * @param status the status of the digging
* @param success true to notify of a success, false otherwise * @param success true to notify of a success, false otherwise
*/ */
private static void sendAcknowledgePacket(Player player, BlockPosition blockPosition, int blockStateId, private static void sendAcknowledgePacket(@NotNull Player player, @NotNull BlockPosition blockPosition, int blockStateId,
ClientPlayerDiggingPacket.Status status, boolean success) { @NotNull ClientPlayerDiggingPacket.Status status, boolean success) {
AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket(); AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket();
acknowledgePlayerDiggingPacket.blockPosition = blockPosition; acknowledgePlayerDiggingPacket.blockPosition = blockPosition;
acknowledgePlayerDiggingPacket.blockStateId = blockStateId; acknowledgePlayerDiggingPacket.blockStateId = blockStateId;