Skip to content

Commit

Permalink
完善一些处理
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed May 6, 2024
1 parent 86803c9 commit a168839
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 60 deletions.
38 changes: 29 additions & 9 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -3217,21 +3229,27 @@ 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;
}
}
}

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()) {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockOreCoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/block/BlockOreGoldNether.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down Expand Up @@ -52,4 +54,9 @@ public Item[] getDrops(Item item, Player player) {
}
return new Item[0];
}

@Override
public int getDropExp() {
return ThreadLocalRandom.current().nextInt(2);
}
}
13 changes: 12 additions & 1 deletion src/main/java/cn/nukkit/block/BlockStemMelon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/cn/nukkit/block/BlockStemPumpkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
}
}
28 changes: 14 additions & 14 deletions src/main/java/cn/nukkit/entity/item/EntityPainting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/inventory/DoubleChestInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/inventory/InventoryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/cn/nukkit/inventory/PlayerInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -333,7 +342,6 @@ public boolean clear(int index, boolean send) {

this.onSlotChange(index, old, newItem, send);
}

return true;
}

Expand All @@ -342,7 +350,6 @@ public Item[] getArmorContents() {
for (int i = 0; i < 4; i++) {
armor[i] = this.getItem(this.getSize() + i);
}

return armor;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/item/enchantment/Enchantment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@ToString
public class GUIDataPickItemPacket extends DataPacket {

public String itemName;
public String itemEffectName = "";
public int hotbarSlot;

@Override
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit a168839

Please sign in to comment.