mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 02:57:37 +01:00
Cleanup & remove unnecessary calls in PlayerDiggingListener
This commit is contained in:
parent
7a4f3672de
commit
4bb27c305f
@ -17,6 +17,7 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket;
|
||||
import net.minestom.server.potion.Potion;
|
||||
import net.minestom.server.potion.PotionEffect;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
@ -29,19 +30,14 @@ public class PlayerDiggingListener {
|
||||
final ClientPlayerDiggingPacket.Status status = packet.status;
|
||||
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();
|
||||
|
||||
if (instance == null)
|
||||
return;
|
||||
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
if (status == ClientPlayerDiggingPacket.Status.STARTED_DIGGING) {
|
||||
|
||||
switch (status) {
|
||||
case STARTED_DIGGING:
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
final boolean instantBreak = player.isCreative() ||
|
||||
player.isInstantBreak() ||
|
||||
Block.fromStateId(blockStateId).breaksInstantaneously();
|
||||
@ -50,7 +46,7 @@ public class PlayerDiggingListener {
|
||||
// No need to check custom block
|
||||
breakBlock(instance, player, blockPosition, blockStateId);
|
||||
} else {
|
||||
final CustomBlock customBlock = instance.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
||||
final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
|
||||
final int customBlockId = customBlock == null ? 0 : customBlock.getCustomBlockId();
|
||||
|
||||
PlayerStartDiggingEvent playerStartDiggingEvent = new PlayerStartDiggingEvent(player, blockPosition, blockStateId, customBlockId);
|
||||
@ -73,15 +69,19 @@ public class PlayerDiggingListener {
|
||||
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CANCELLED_DIGGING:
|
||||
|
||||
} else if (status == ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING) {
|
||||
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
// Remove custom block target
|
||||
player.resetTargetBlock();
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, blockStateId,
|
||||
ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING, true);
|
||||
break;
|
||||
case FINISHED_DIGGING:
|
||||
|
||||
} else if (status == ClientPlayerDiggingPacket.Status.FINISHED_DIGGING) {
|
||||
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
|
||||
if (customBlock != null && customBlock.enableCustomBreakDelay()) {
|
||||
// Is not supposed to happen, probably a bug
|
||||
@ -91,12 +91,14 @@ public class PlayerDiggingListener {
|
||||
// Vanilla block
|
||||
breakBlock(instance, player, blockPosition, blockStateId);
|
||||
}
|
||||
break;
|
||||
case DROP_ITEM_STACK:
|
||||
|
||||
} else if (status == ClientPlayerDiggingPacket.Status.DROP_ITEM_STACK) {
|
||||
|
||||
final ItemStack droppedItemStack = player.getInventory().getItemInMainHand();
|
||||
dropItem(player, droppedItemStack, ItemStack.getAirItem());
|
||||
break;
|
||||
case DROP_ITEM:
|
||||
|
||||
} else if (status == ClientPlayerDiggingPacket.Status.DROP_ITEM) {
|
||||
|
||||
final int dropAmount = 1;
|
||||
|
||||
ItemStack handItem = player.getInventory().getItemInMainHand();
|
||||
@ -117,8 +119,9 @@ public class PlayerDiggingListener {
|
||||
|
||||
dropItem(player, droppedItemStack2, handItem);
|
||||
}
|
||||
break;
|
||||
case UPDATE_ITEM_STATE:
|
||||
|
||||
} else if (status == ClientPlayerDiggingPacket.Status.UPDATE_ITEM_STATE) {
|
||||
|
||||
player.refreshEating(false);
|
||||
ItemUpdateStateEvent itemUpdateStateEvent = player.callItemUpdateStateEvent(false);
|
||||
|
||||
@ -129,14 +132,18 @@ public class PlayerDiggingListener {
|
||||
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffHand, false);
|
||||
}
|
||||
|
||||
break;
|
||||
case SWAP_ITEM_HAND:
|
||||
} 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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
if (player.dropItem(droppedItem)) {
|
||||
playerInventory.setItemInMainHand(handItem);
|
||||
@ -185,7 +193,7 @@ public class PlayerDiggingListener {
|
||||
*
|
||||
* @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);
|
||||
|
||||
EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
|
||||
@ -208,7 +216,7 @@ public class PlayerDiggingListener {
|
||||
*
|
||||
* @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)) {
|
||||
playersEffect.remove(player);
|
||||
|
||||
@ -228,8 +236,8 @@ public class PlayerDiggingListener {
|
||||
* @param status the status of the digging
|
||||
* @param success true to notify of a success, false otherwise
|
||||
*/
|
||||
private static void sendAcknowledgePacket(Player player, BlockPosition blockPosition, int blockStateId,
|
||||
ClientPlayerDiggingPacket.Status status, boolean success) {
|
||||
private static void sendAcknowledgePacket(@NotNull Player player, @NotNull BlockPosition blockPosition, int blockStateId,
|
||||
@NotNull ClientPlayerDiggingPacket.Status status, boolean success) {
|
||||
AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket();
|
||||
acknowledgePlayerDiggingPacket.blockPosition = blockPosition;
|
||||
acknowledgePlayerDiggingPacket.blockStateId = blockStateId;
|
||||
|
Loading…
Reference in New Issue
Block a user