From a168839572e7ee216f70ba58f8dd05522f779c68 Mon Sep 17 00:00:00 2001 From: Woder <17339354+wode490390@users.noreply.github.com> Date: Mon, 6 May 2024 20:02:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=80=E4=BA=9B=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/nukkit/Player.java | 38 ++++++++++++---- .../java/cn/nukkit/block/BlockOreCoal.java | 2 +- .../cn/nukkit/block/BlockOreGoldNether.java | 7 +++ .../java/cn/nukkit/block/BlockStemMelon.java | 13 +++++- .../cn/nukkit/block/BlockStemPumpkin.java | 13 +++++- .../nukkit/entity/item/EntityEndCrystal.java | 14 ++++++ .../cn/nukkit/entity/item/EntityPainting.java | 28 ++++++------ .../inventory/DoubleChestInventory.java | 4 +- .../cn/nukkit/inventory/InventoryType.java | 2 + .../cn/nukkit/inventory/PlayerInventory.java | 43 +++++++++++-------- .../nukkit/item/enchantment/Enchantment.java | 5 ++- .../protocol/GUIDataPickItemPacket.java | 4 ++ .../network/protocol/InteractPacket.java | 10 +++++ .../network/protocol/UpdateTradePacket.java | 27 ++++++------ 14 files changed, 150 insertions(+), 60 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 5a175f7a57b..df3c47d78dc 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -2183,9 +2183,12 @@ public void checkInteractNearby() { int interactDistance = isCreative() ? 5 : 3; EntityInteractable onInteract = getEntityPlayerLookingAt(interactDistance); if (onInteract != null) { - setButtonText(onInteract.getInteractButtonText(this)); + String text = onInteract.getInteractButtonText(this); + setButtonText(text); + setDataProperty(new ByteEntityData(DATA_CAN_RIDE_TARGET, onInteract != riding && onInteract instanceof EntityRideable && ("action.interact.mount".equals(text) || text.startsWith("action.interact.ride.")))); } else { setButtonText(""); + setDataProperty(new ByteEntityData(DATA_CAN_RIDE_TARGET, false)); } } @@ -3126,6 +3129,7 @@ public void onCompletion(Server server) { if (interactPacket.target == 0 && interactPacket.action == InteractPacket.ACTION_MOUSEOVER) { this.lookAtEntity = null; this.setButtonText(""); + setDataProperty(new ByteEntityData(DATA_CAN_RIDE_TARGET, false)); break; } @@ -3141,14 +3145,14 @@ public void onCompletion(Server server) { break; } - //item = this.inventory.getItemInHand(); - switch (interactPacket.action) { case InteractPacket.ACTION_MOUSEOVER: this.lookAtEntity = targetEntity; - if (targetEntity instanceof EntityInteractable) { - this.setButtonText(((EntityInteractable) targetEntity).getInteractButtonText(this)); + if (targetEntity instanceof EntityInteractable interactable) { + String text = interactable.getInteractButtonText(this); + this.setButtonText(text); + setDataProperty(new ByteEntityData(DATA_CAN_RIDE_TARGET, targetEntity != riding && targetEntity instanceof EntityRideable && ("action.interact.mount".equals(text) || text.startsWith("action.interact.ride.")))); } this.getServer().getPluginManager().callEvent(new PlayerMouseOverEntityEvent(this, targetEntity)); @@ -3200,11 +3204,19 @@ public void onCompletion(Server server) { this.server.getPluginManager().callEvent(pickEvent); if (!pickEvent.isCancelled()) { + item = pickEvent.getItem(); + + GUIDataPickItemPacket pk = new GUIDataPickItemPacket(); + pk.itemName = item.getName(); + pk.itemEffectName = ""; + boolean itemExists = false; int itemSlot = -1; for (int slot = 0; slot < this.inventory.getHotbarSize(); slot++) { - if (this.inventory.getItem(slot).equals(pickEvent.getItem())) { + if (this.inventory.getItem(slot).equals(item)) { if (slot < this.inventory.getHotbarSize()) { + pk.hotbarSlot = slot; + dataPacket(pk); this.inventory.setHeldItemSlot(slot); } else { itemSlot = slot; @@ -3217,13 +3229,17 @@ public void onCompletion(Server server) { for (int slot = 0; slot < this.inventory.getHotbarSize(); slot++) { if (this.inventory.getItem(slot).isNull()) { if (!itemExists && this.isCreative()) { + pk.hotbarSlot = slot; + dataPacket(pk); this.inventory.setHeldItemSlot(slot); - this.inventory.setItemInHand(pickEvent.getItem()); + this.inventory.setItemInHand(item); break packetswitch; } else if (itemSlot > -1) { + pk.hotbarSlot = slot; + dataPacket(pk); this.inventory.setHeldItemSlot(slot); this.inventory.setItemInHand(this.inventory.getItem(itemSlot)); - this.inventory.clear(itemSlot, true); + this.inventory.clear(itemSlot); break packetswitch; } } @@ -3231,7 +3247,9 @@ public void onCompletion(Server server) { if (!itemExists && this.isCreative()) { Item itemInHand = this.inventory.getItemInHand(); - this.inventory.setItemInHand(pickEvent.getItem()); + pk.hotbarSlot = inventory.getHeldItemIndex(); + dataPacket(pk); + this.inventory.setItemInHand(item); if (!this.inventory.isFull()) { for (int slot = 0; slot < this.inventory.getSize(); slot++) { if (this.inventory.getItem(slot).isNull()) { @@ -3242,6 +3260,8 @@ public void onCompletion(Server server) { } } else if (itemSlot > -1) { Item itemInHand = this.inventory.getItemInHand(); + pk.hotbarSlot = inventory.getHeldItemIndex(); + dataPacket(pk); this.inventory.setItemInHand(this.inventory.getItem(itemSlot)); this.inventory.setItem(itemSlot, itemInHand); } diff --git a/src/main/java/cn/nukkit/block/BlockOreCoal.java b/src/main/java/cn/nukkit/block/BlockOreCoal.java index 6dfa421eaa0..3c401c45046 100644 --- a/src/main/java/cn/nukkit/block/BlockOreCoal.java +++ b/src/main/java/cn/nukkit/block/BlockOreCoal.java @@ -67,7 +67,7 @@ public Item[] getDrops(Item item, Player player) { @Override public int getDropExp() { - return ThreadLocalRandom.current().nextInt(0, 3); + return ThreadLocalRandom.current().nextInt(3); } @Override diff --git a/src/main/java/cn/nukkit/block/BlockOreGoldNether.java b/src/main/java/cn/nukkit/block/BlockOreGoldNether.java index 7ba4e5f9e6d..155d740cafc 100644 --- a/src/main/java/cn/nukkit/block/BlockOreGoldNether.java +++ b/src/main/java/cn/nukkit/block/BlockOreGoldNether.java @@ -4,6 +4,8 @@ import cn.nukkit.item.Item; import cn.nukkit.item.ItemTool; +import java.util.concurrent.ThreadLocalRandom; + public class BlockOreGoldNether extends BlockSolid { public BlockOreGoldNether() { } @@ -52,4 +54,9 @@ public Item[] getDrops(Item item, Player player) { } return new Item[0]; } + + @Override + public int getDropExp() { + return ThreadLocalRandom.current().nextInt(2); + } } diff --git a/src/main/java/cn/nukkit/block/BlockStemMelon.java b/src/main/java/cn/nukkit/block/BlockStemMelon.java index eb75b516f98..6095d0bf64f 100644 --- a/src/main/java/cn/nukkit/block/BlockStemMelon.java +++ b/src/main/java/cn/nukkit/block/BlockStemMelon.java @@ -35,8 +35,19 @@ public Item toItem(boolean addUserData) { @Override public Item[] getDrops(Item item, Player player) { + int count = 0; + int growth = getGrowth(); + ThreadLocalRandom random = ThreadLocalRandom.current(); + for (int i = 0; i < 3; i++) { + if (random.nextInt(15) <= growth) { + count++; + } + } + if (count == 0) { + return new Item[0]; + } return new Item[]{ - Item.get(Item.MELON_SEEDS, 0, ThreadLocalRandom.current().nextInt(0, 4)) + Item.get(Item.MELON_SEEDS, 0, count) }; } diff --git a/src/main/java/cn/nukkit/block/BlockStemPumpkin.java b/src/main/java/cn/nukkit/block/BlockStemPumpkin.java index 352f3441148..cbcd4b305b9 100644 --- a/src/main/java/cn/nukkit/block/BlockStemPumpkin.java +++ b/src/main/java/cn/nukkit/block/BlockStemPumpkin.java @@ -35,8 +35,19 @@ public Item toItem(boolean addUserData) { @Override public Item[] getDrops(Item item, Player player) { + int count = 0; + int growth = getGrowth(); + ThreadLocalRandom random = ThreadLocalRandom.current(); + for (int i = 0; i < 3; i++) { + if (random.nextInt(15) <= growth) { + count++; + } + } + if (count == 0) { + return new Item[0]; + } return new Item[]{ - Item.get(Item.PUMPKIN_SEEDS, 0, ThreadLocalRandom.current().nextInt(0, 4)) + Item.get(Item.PUMPKIN_SEEDS, 0, count) }; } diff --git a/src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java b/src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java index 994630d46d0..32fa5d16102 100644 --- a/src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java +++ b/src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java @@ -4,11 +4,13 @@ import cn.nukkit.entity.Entity; import cn.nukkit.entity.EntityExplosive; import cn.nukkit.entity.EntityID; +import cn.nukkit.entity.data.Vector3fEntityData; import cn.nukkit.event.entity.EntityDamageEvent; import cn.nukkit.level.Explosion; import cn.nukkit.level.GameRule; import cn.nukkit.level.Position; import cn.nukkit.level.format.FullChunk; +import cn.nukkit.math.Vector3f; import cn.nukkit.nbt.tag.CompoundTag; /** @@ -106,4 +108,16 @@ public void spawnTo(Player player) { super.spawnTo(player); } + + public Vector3f getTargetPos() { + return getDataPropertyVector3f(DATA_BLOCK_TARGET); + } + + public void setTargetPos(Vector3f pos) { + setTargetPos(pos.x, pos.y, pos.z); + } + + public void setTargetPos(float x, float y, float z) { + setDataProperty(new Vector3fEntityData(DATA_BLOCK_TARGET, x, y, z)); + } } diff --git a/src/main/java/cn/nukkit/entity/item/EntityPainting.java b/src/main/java/cn/nukkit/entity/item/EntityPainting.java index 7b090cd43f9..a7f8da01b0a 100644 --- a/src/main/java/cn/nukkit/entity/item/EntityPainting.java +++ b/src/main/java/cn/nukkit/entity/item/EntityPainting.java @@ -372,28 +372,28 @@ public enum Motive { FIRE("Fire", 2, 2, true), WATER("Water", 2, 2, true), WIND("Wind", 2, 2, true), +// BOUQUET("Bouquet", 3, 3), +// CAVEBIRD("Cavebird", 3, 3), +// COTAN("Cotan", 3, 3), +// ENDBOSS("Endboss", 3, 3), +// FERN("Fern", 3, 3), +// OWLEMONS("Owlemons", 3, 3), +// SUNFLOWERS("Sunflowers", 3, 3), +// TIDES("Tides", 3, 3), +// BACKYARD("Backyard", 3, 4), +// POND("Pond", 3, 4), FIGHTERS("Fighters", 4, 2), +// CHAINGING("Chainging", 4, 2), +// FINDING("Finding", 4, 2), +// LOWMIST("Lowmist", 4, 2), +// PASSAGE("Passage", 4, 2), SKELETON("Skeleton", 4, 3), DONKEY_KONG("DonkeyKong", 4, 3), POINTER("Pointer", 4, 4), PIG_SCENE("Pigscene", 4, 4), BURNING_SKULL("BurningSkull", 4, 4), // UNPACKED("Unpacked", 4, 4), -// BOUQUET("Bouquet", 4, 4), -// CAVEBIRD("Cavebird", 4, 4), -// COTAN("Cotan", 4, 4), -// ENDBOSS("Endboss", 4, 4), -// FERN("Fern", 4, 4), // ORB("Orb", 4, 4), -// PASSAGE("Passage", 4, 4), -// TIDES("Tides", 4, 4), -// SUNFLOWERS("Sunflowers", 4, 4), -// BACKYARD("Backyard", 4, 5), -// POND("Pond", 4, 5), -// CHAINGING("Chainging", 5, 3), -// FINDING("Finding", 5, 3), -// LOWMIST("Lowmist", 5, 3), -// OWLEMONS("Owlemons", 5, 3), ; public final String title; diff --git a/src/main/java/cn/nukkit/inventory/DoubleChestInventory.java b/src/main/java/cn/nukkit/inventory/DoubleChestInventory.java index 7e2b7872094..8dcf198e726 100644 --- a/src/main/java/cn/nukkit/inventory/DoubleChestInventory.java +++ b/src/main/java/cn/nukkit/inventory/DoubleChestInventory.java @@ -69,8 +69,8 @@ public boolean setItem(int index, Item item, boolean send) { } @Override - public boolean clear(int index) { - return index < this.left.getSize() ? this.left.clear(index) : this.right.clear(index - this.right.getSize()); + public boolean clear(int index, boolean send) { + return index < this.left.getSize() ? this.left.clear(index, send) : this.right.clear(index - this.right.getSize(), send); } @Override diff --git a/src/main/java/cn/nukkit/inventory/InventoryType.java b/src/main/java/cn/nukkit/inventory/InventoryType.java index 542119c59c8..97f02337483 100644 --- a/src/main/java/cn/nukkit/inventory/InventoryType.java +++ b/src/main/java/cn/nukkit/inventory/InventoryType.java @@ -38,6 +38,8 @@ public enum InventoryType { SMITHING_TABLE(3, "Smithing Table", ContainerType.SMITHING_TABLE), BOAT_CHEST(27, "Boat with Chest", /*ContainerType.CHEST_BOAT*/ContainerType.CONTAINER), HORSE(16, "Horse", ContainerType.HORSE), + TRADE(3, "Villager", ContainerType.TRADING), + CRAFTER(9, "Crafter", ContainerType.CRAFTER), COMMAND_BLOCK(0, "Command Block", ContainerType.COMMAND_BLOCK), STRUCTURE_EDITOR(0, "Structure Block", ContainerType.STRUCTURE_EDITOR), JIGSAW_EDITOR(0, "Jigsaw Block", ContainerType.JIGSAW_EDITOR), diff --git a/src/main/java/cn/nukkit/inventory/PlayerInventory.java b/src/main/java/cn/nukkit/inventory/PlayerInventory.java index 65653e42a26..255e5dd4d9d 100644 --- a/src/main/java/cn/nukkit/inventory/PlayerInventory.java +++ b/src/main/java/cn/nukkit/inventory/PlayerInventory.java @@ -97,8 +97,13 @@ public void setHeldItemIndex(int index, boolean send) { if (index >= 0 && index < this.getHotbarSize()) { this.itemInHandIndex = index; - if (this.getHolder() instanceof Player && send) { - this.sendHeldItem((Player) this.getHolder()); + if (this.getHolder() instanceof Player player && send) { +// this.sendHeldItem(player); + PlayerHotbarPacket packet = new PlayerHotbarPacket(); + packet.selectedHotbarSlot = index; + packet.windowId = ContainerIds.INVENTORY; + packet.selectHotbarSlot = true; + player.dataPacket(packet); } this.sendHeldItem(this.getHolder().getViewers().values()); @@ -129,8 +134,13 @@ public void setHeldItemSlot(int slot) { this.itemInHandIndex = slot; - if (this.getHolder() instanceof Player) { - this.sendHeldItem((Player) this.getHolder()); + if (this.getHolder() instanceof Player player) { +// this.sendHeldItem(player); + PlayerHotbarPacket packet = new PlayerHotbarPacket(); + packet.selectedHotbarSlot = slot; + packet.windowId = ContainerIds.INVENTORY; + packet.selectHotbarSlot = true; + player.dataPacket(packet); } this.sendHeldItem(this.getViewers()); @@ -248,11 +258,6 @@ public boolean setBoots(Item boots, boolean ignoreArmorEvents) { return this.setItem(this.getSize() + 3, boots, true, ignoreArmorEvents); } - @Override - public boolean setItem(int index, Item item) { - return setItem(index, item, true, false); - } - @Override public boolean setItem(int index, Item item, boolean send) { return setItem(index, item, send, false); @@ -267,14 +272,16 @@ private boolean setItem(int index, Item item, boolean send, boolean ignoreArmorE } //Armor change - if (!ignoreArmorEvents && index >= this.getSize()) { - EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), this.getItem(index), item, index); - Server.getInstance().getPluginManager().callEvent(ev); - if (ev.isCancelled() && this.getHolder() != null) { - this.sendArmorSlot(index, this.getViewers()); - return false; + if (index >= this.getSize()) { + if (!ignoreArmorEvents) { + EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), this.getItem(index), item, index); + Server.getInstance().getPluginManager().callEvent(ev); + if (ev.isCancelled() && this.getHolder() != null) { + this.sendArmorSlot(index, this.getViewers()); + return false; + } + item = ev.getNewItem(); } - item = ev.getNewItem(); } else { EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), this.getItem(index), item, index); Server.getInstance().getPluginManager().callEvent(ev); @@ -284,9 +291,11 @@ private boolean setItem(int index, Item item, boolean send, boolean ignoreArmorE } item = ev.getNewItem(); } + Item old = this.getItem(index); Item newItem = item.clone(); this.slots.put(index, newItem); + this.onSlotChange(index, old, newItem, send); return true; } @@ -333,7 +342,6 @@ public boolean clear(int index, boolean send) { this.onSlotChange(index, old, newItem, send); } - return true; } @@ -342,7 +350,6 @@ public Item[] getArmorContents() { for (int i = 0; i < 4; i++) { armor[i] = this.getItem(this.getSize() + i); } - return armor; } diff --git a/src/main/java/cn/nukkit/item/enchantment/Enchantment.java b/src/main/java/cn/nukkit/item/enchantment/Enchantment.java index 8b4eec7d2b3..13c48b6ba70 100644 --- a/src/main/java/cn/nukkit/item/enchantment/Enchantment.java +++ b/src/main/java/cn/nukkit/item/enchantment/Enchantment.java @@ -172,10 +172,11 @@ protected Enchantment clone() { public static final String[] words = {"the", "elder", "scrolls", "klaatu", "berata", "niktu", "xyzzy", "bless", "curse", "light", "darkness", "fire", "air", "earth", "water", "hot", "dry", "cold", "wet", "ignite", "snuff", "embiggen", "twist", "shorten", "stretch", "fiddle", "destroy", "imbue", "galvanize", "enchant", "free", "limited", "range", "of", "towards", "inside", "sphere", "cube", "self", "other", "ball", "mental", "physical", "grow", "shrink", "demon", "elemental", "spirit", "animal", "creature", "beast", "humanoid", "undead", "fresh", "stale"}; public static String getRandomName() { - int count = ThreadLocalRandom.current().nextInt(3, 6); + ThreadLocalRandom random = ThreadLocalRandom.current(); + int count = random.nextInt(3, 6); Set set = new ObjectOpenHashSet<>(); while (set.size() < count) { - set.add(Enchantment.words[ThreadLocalRandom.current().nextInt(0, Enchantment.words.length)]); + set.add(Enchantment.words[random.nextInt(Enchantment.words.length)]); } String[] words = set.toArray(new String[0]); diff --git a/src/main/java/cn/nukkit/network/protocol/GUIDataPickItemPacket.java b/src/main/java/cn/nukkit/network/protocol/GUIDataPickItemPacket.java index fe7a108c898..433a6d5a9bb 100644 --- a/src/main/java/cn/nukkit/network/protocol/GUIDataPickItemPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/GUIDataPickItemPacket.java @@ -5,6 +5,8 @@ @ToString public class GUIDataPickItemPacket extends DataPacket { + public String itemName; + public String itemEffectName = ""; public int hotbarSlot; @Override @@ -15,6 +17,8 @@ public int pid() { @Override public void encode() { this.reset(); + this.putString(this.itemName); + this.putString(this.itemEffectName); this.putLInt(this.hotbarSlot); } diff --git a/src/main/java/cn/nukkit/network/protocol/InteractPacket.java b/src/main/java/cn/nukkit/network/protocol/InteractPacket.java index 8f08c23005d..bc4ec572ed2 100644 --- a/src/main/java/cn/nukkit/network/protocol/InteractPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/InteractPacket.java @@ -21,10 +21,20 @@ public class InteractPacket extends DataPacket { public int action; public long target; + public float x; + public float y; + public float z; + @Override public void decode() { this.action = this.getByte(); this.target = this.getEntityRuntimeId(); + + if (this.action == ACTION_MOUSEOVER) { + this.x = this.getLFloat(); + this.y = this.getLFloat(); + this.z = this.getLFloat(); + } } @Override diff --git a/src/main/java/cn/nukkit/network/protocol/UpdateTradePacket.java b/src/main/java/cn/nukkit/network/protocol/UpdateTradePacket.java index f00ec9de580..a254eca3315 100644 --- a/src/main/java/cn/nukkit/network/protocol/UpdateTradePacket.java +++ b/src/main/java/cn/nukkit/network/protocol/UpdateTradePacket.java @@ -8,14 +8,17 @@ public class UpdateTradePacket extends DataPacket { public static final int NETWORK_ID = ProtocolInfo.UPDATE_TRADE_PACKET; - public byte windowId; - public byte windowType = ContainerType.TRADING; - public int unknownVarInt1; - public int unknownVarInt2; - public boolean isWilling; + public int windowId; + public int windowType = ContainerType.TRADING; + public int windowSize; // hardcoded to 0 + public int tradeTier; + @Deprecated + public boolean recipeAddedOnUpdate; public long trader; public long player; - public String displayName; + public String displayName = ""; + public boolean newTradingUi; + public boolean usingEconomyTrade; public byte[] offers; @Override @@ -31,13 +34,13 @@ public void decode() { @Override public void encode() { this.reset(); - this.putByte(windowId); - this.putByte(windowType); - this.putVarInt(unknownVarInt1); - this.putVarInt(unknownVarInt2); - this.putBoolean(isWilling); - this.putEntityUniqueId(player); + this.putByte((byte) windowId); + this.putByte((byte) windowType); + this.putVarInt(windowSize); + this.putVarInt(usingEconomyTrade ? 40 : 0); // Merchant Timer + this.putBoolean(recipeAddedOnUpdate); this.putEntityUniqueId(trader); + this.putEntityUniqueId(player); this.putString(displayName); this.put(this.offers); }