Added ItemUpdateStateEvent + batch file clean

This commit is contained in:
Felix Cravic 2020-04-24 17:43:35 +02:00
parent 90925b4967
commit 2f6205b577
7 changed files with 65 additions and 12 deletions

View File

@ -148,7 +148,7 @@ public class PlayerInit {
});
player.setEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.SURVIVAL);
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 75, 0));
ItemStack item = new ItemStack((short) 1, (byte) 43);

View File

@ -0,0 +1,25 @@
package net.minestom.server.event;
import net.minestom.server.item.ItemStack;
public class ItemUpdateStateEvent extends Event {
private ItemStack itemStack;
private boolean handAnimation;
public ItemUpdateStateEvent(ItemStack itemStack) {
this.itemStack = itemStack;
}
public ItemStack getItemStack() {
return itemStack;
}
public void setHandAnimation(boolean handAnimation) {
this.handAnimation = handAnimation;
}
public boolean hasHandAnimation() {
return handAnimation;
}
}

View File

@ -22,7 +22,6 @@ import java.io.File;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
@ -37,7 +36,7 @@ public class InstanceContainer extends Instance {
private ChunkGenerator chunkGenerator;
private Map<Long, Chunk> chunks = new ConcurrentHashMap<>();
private AtomicBoolean autoChunkLoad = new AtomicBoolean(false);
private boolean autoChunkLoad;
protected InstanceContainer(UUID uniqueId, File folder) {
super(uniqueId);
@ -331,12 +330,12 @@ public class InstanceContainer extends Instance {
@Override
public void enableAutoChunkLoad(boolean enable) {
this.autoChunkLoad.set(enable);
this.autoChunkLoad = enable;
}
@Override
public boolean hasEnabledAutoChunkLoad() {
return autoChunkLoad.get();
return autoChunkLoad;
}
protected void addSharedInstance(SharedInstance sharedInstance) {

View File

@ -72,7 +72,6 @@ public class BlockBatch implements InstanceBatch {
});
if (isLast) {
// data.clear();
if (callback != null)
callback.run();
}

View File

@ -19,7 +19,10 @@ public class ChunkBatch implements InstanceBatch {
private InstanceContainer instance;
private Chunk chunk;
private List<BlockData> dataList = Collections.synchronizedList(new ArrayList<>());
// Give it the max capacity by default (avoid resizing)
private List<BlockData> dataList =
Collections.synchronizedList(new ArrayList<>(
Chunk.CHUNK_SIZE_X * Chunk.CHUNK_SIZE_Y * Chunk.CHUNK_SIZE_Z));
public ChunkBatch(InstanceContainer instance, Chunk chunk) {
this.instance = instance;

View File

@ -980,6 +980,18 @@ public enum Material {
}
}
public boolean hasState() {
switch (this) {
case BOW:
case TRIDENT:
case CROSSBOW:
case SHIELD:
return true;
}
return isFood();
}
public short getId() {
return materialId;
}

View File

@ -2,12 +2,14 @@ package net.minestom.server.listener;
import net.minestom.server.entity.GameMode;
import net.minestom.server.entity.Player;
import net.minestom.server.event.ItemUpdateStateEvent;
import net.minestom.server.event.PlayerStartDiggingEvent;
import net.minestom.server.event.PlayerSwapItemEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.server.play.AcknowledgePlayerDiggingPacket;
import net.minestom.server.network.packet.server.play.EntityEffectPacket;
@ -19,6 +21,10 @@ public class PlayerDiggingListener {
public static void playerDiggingListener(ClientPlayerDiggingPacket packet, Player player) {
ClientPlayerDiggingPacket.Status status = packet.status;
BlockPosition blockPosition = packet.blockPosition;
PlayerInventory playerInventory = player.getInventory();
ItemStack mainHand = playerInventory.getItemInMainHand();
ItemStack offHand = playerInventory.getItemInOffHand();
switch (status) {
case STARTED_DIGGING:
if (player.getGameMode() == GameMode.CREATIVE) {
@ -79,14 +85,23 @@ public class PlayerDiggingListener {
dropItem(player, droppedItemStack2, handItem);
break;
case UPDATE_ITEM_STATE:
player.refreshActiveHand(false, false, false);
Material mainHandMat = Material.fromId(mainHand.getMaterialId());
Material offHandMat = Material.fromId(offHand.getMaterialId());
boolean isOffhand = offHandMat.hasState();
ItemStack updatedItem = isOffhand ? offHand :
mainHandMat.hasState() ? mainHand : null;
if (updatedItem == null) // No item with state, cancel
return;
ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(updatedItem);
player.callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent);
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffhand, false);
player.sendPacketToViewers(player.getMetadataPacket());
break;
case SWAP_ITEM_HAND:
PlayerInventory playerInventory = player.getInventory();
ItemStack mainHand = playerInventory.getItemInMainHand().clone();
ItemStack offHand = playerInventory.getItemInOffHand().clone();
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand, mainHand);
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand.clone(), mainHand.clone());
player.callCancellableEvent(PlayerSwapItemEvent.class, swapItemEvent, () -> {
playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem());
playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem());