Skip to content

Commit

Permalink
Merge branch 'master' into redrak
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/cn/nukkit/Player.java
#	src/main/java/cn/nukkit/inventory/PlayerInventory.java
#	src/main/java/cn/nukkit/network/RakNetInterface.java
  • Loading branch information
Sleepybear committed Jul 14, 2020
2 parents 0477574 + 6a43a0e commit 69c992f
Show file tree
Hide file tree
Showing 25 changed files with 494 additions and 147 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ Links

Build JAR file
-------------
- `git clone https://github.com/NukkitX/Nukkit`
- `git clone https://github.com/CloudburstMC/Nukkit`
- `cd Nukkit`
- `git submodule update --init`
- `chmod +x mvnw`
- `./mvnw clean package`

The compiled JAR can be found in the `target/` directory.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
<junit.platform.version>1.0.0-M4</junit.platform.version>
<log4j2.version>2.11.1</log4j2.version>
<log4j2.version>2.13.3</log4j2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jline.version>3.9.0</jline.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/Nukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Nukkit {

public final static Properties GIT_INFO = getGitInfo();
public final static String VERSION = getVersion();
public final static String API_VERSION = "1.0.10";
public final static String API_VERSION = "1.0.11";
public final static String CODENAME = "";
@Deprecated
public final static String MINECRAFT_VERSION = ProtocolInfo.MINECRAFT_VERSION;
Expand Down
98 changes: 65 additions & 33 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import cn.nukkit.form.window.FormWindowCustom;
import cn.nukkit.inventory.*;
import cn.nukkit.inventory.transaction.CraftingTransaction;
import cn.nukkit.inventory.transaction.EnchantTransaction;
import cn.nukkit.inventory.transaction.InventoryTransaction;
import cn.nukkit.inventory.transaction.action.InventoryAction;
import cn.nukkit.inventory.transaction.data.ReleaseItemData;
Expand Down Expand Up @@ -137,6 +138,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde

protected final BiMap<Integer, Inventory> windowIndex = windows.inverse();
protected final Set<Integer> permanentWindows = new IntOpenHashSet();
private boolean inventoryOpen;

protected int messageCounter = 2;

Expand All @@ -151,6 +153,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected PlayerUIInventory playerUIInventory;
protected CraftingGrid craftingGrid;
protected CraftingTransaction craftingTransaction;
protected EnchantTransaction enchantTransaction;

public long creationTime = 0;

Expand Down Expand Up @@ -1053,7 +1056,7 @@ public boolean dataPacket(DataPacket packet) {
return false;
}

if (log.isTraceEnabled() && !(packet instanceof BatchPacket)) {
if (log.isTraceEnabled() && !server.isIgnoredPacket(packet.getClass())) {
log.trace("Outbound {}: {}", this.getName(), packet);
}

Expand Down Expand Up @@ -2152,7 +2155,7 @@ public void handleDataPacket(DataPacket packet) {
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '_' || c == ' '
) {
) {
continue;
}

Expand Down Expand Up @@ -2269,7 +2272,7 @@ public void onCompletion(Server server) {
PlayerSkinPacket skinPacket = (PlayerSkinPacket) packet;
Skin skin = skinPacket.skin;

if(!skin.isValid()) {
if (!skin.isValid()) {
break;
}

Expand Down Expand Up @@ -2635,7 +2638,10 @@ public void onCompletion(Server server) {
break;
case InteractPacket.ACTION_OPEN_INVENTORY:
if (targetEntity.getId() != this.getId()) break;
this.inventory.open(this);
if (!this.inventoryOpen) {
this.inventory.open(this);
this.inventoryOpen = true;
}
break;
}
break;
Expand Down Expand Up @@ -2668,7 +2674,7 @@ public void onCompletion(Server server) {
int itemSlot = -1;
for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (this.inventory.getItem(slot).equals(pickEvent.getItem())) {
if(slot < this.inventory.getHotbarSize()) {
if (slot < this.inventory.getHotbarSize()) {
this.inventory.setHeldItemSlot(slot);
} else {
itemSlot = slot;
Expand All @@ -2678,13 +2684,13 @@ public void onCompletion(Server server) {
}
}

for(int slot = 0; slot < this.inventory.getHotbarSize(); slot++) {
if(this.inventory.getItem(slot).isNull()) {
if(!itemExists && this.isCreative()) {
for (int slot = 0; slot < this.inventory.getHotbarSize(); slot++) {
if (this.inventory.getItem(slot).isNull()) {
if (!itemExists && this.isCreative()) {
this.inventory.setHeldItemSlot(slot);
this.inventory.setItemInHand(pickEvent.getItem());
break packetswitch;
} else if(itemSlot > -1) {
} else if (itemSlot > -1) {
this.inventory.setHeldItemSlot(slot);
this.inventory.setItemInHand(this.inventory.getItem(itemSlot));
this.inventory.clear(itemSlot, true);
Expand All @@ -2693,18 +2699,18 @@ public void onCompletion(Server server) {
}
}

if(!itemExists && this.isCreative()) {
if (!itemExists && this.isCreative()) {
Item itemInHand = this.inventory.getItemInHand();
this.inventory.setItemInHand(pickEvent.getItem());
if(!this.inventory.isFull()) {
for(int slot = 0; slot < this.inventory.getSize(); slot++) {
if(this.inventory.getItem(slot).isNull()) {
if (!this.inventory.isFull()) {
for (int slot = 0; slot < this.inventory.getSize(); slot++) {
if (this.inventory.getItem(slot).isNull()) {
this.inventory.setItem(slot, itemInHand);
break;
}
}
}
} else if(itemSlot > -1) {
} else if (itemSlot > -1) {
Item itemInHand = this.inventory.getItemInHand();
this.inventory.setItemInHand(this.inventory.getItem(itemSlot));
this.inventory.setItem(itemSlot, itemInHand);
Expand Down Expand Up @@ -2764,14 +2770,6 @@ public void onCompletion(Server server) {
this.dataPacket(entityEventPacket);
Server.broadcastPacket(this.getViewers().values(), entityEventPacket);
break;
case EntityEventPacket.ENCHANT:
if (entityEventPacket.eid != this.id) {
break;
}

int levels = entityEventPacket.data; // Sent as negative number of levels lost
if (levels < 0) this.setExperience(this.exp, this.expLevel + levels);
break;
}
break;
case ProtocolInfo.COMMAND_REQUEST_PACKET:
Expand Down Expand Up @@ -2809,15 +2807,14 @@ public void onCompletion(Server server) {
break;
case ProtocolInfo.CONTAINER_CLOSE_PACKET:
ContainerClosePacket containerClosePacket = (ContainerClosePacket) packet;
if (!this.spawned) {
if (!this.spawned || containerClosePacket.windowId == ContainerIds.INVENTORY && !inventoryOpen) {
break;
}

if (this.windowIndex.containsKey(containerClosePacket.windowId)) {
this.server.getPluginManager().callEvent(new InventoryCloseEvent(this.windowIndex.get(containerClosePacket.windowId), this));
if (containerClosePacket.windowId == ContainerIds.INVENTORY) this.inventoryOpen = false;
this.removeWindow(this.windowIndex.get(containerClosePacket.windowId));
} else {
this.windowIndex.remove(containerClosePacket.windowId);
}
if (containerClosePacket.windowId == -1) {
this.craftingType = CRAFTING_SMALL;
Expand Down Expand Up @@ -2972,24 +2969,51 @@ public void onCompletion(Server server) {
}
}

if (this.craftingTransaction.getPrimaryOutput() != null) {
if (this.craftingTransaction.getPrimaryOutput() != null && this.craftingTransaction.canExecute()) {
//we get the actions for this in several packets, so we can't execute it until we get the result

this.craftingTransaction.execute();
this.craftingTransaction = null;
}

return;
} else if (transactionPacket.isEnchantingPart) {
if (this.enchantTransaction == null) {
this.enchantTransaction = new EnchantTransaction(this, actions);
} else {
for (InventoryAction action : actions) {
this.enchantTransaction.addAction(action);
}
}
if (this.enchantTransaction.canExecute()) {
this.enchantTransaction.execute();
this.enchantTransaction = null;
}
return;
} else if (this.craftingTransaction != null) {
if(craftingTransaction.checkForCraftingPart(actions)){
if (craftingTransaction.checkForCraftingPart(actions)) {
for (InventoryAction action : actions) {
craftingTransaction.addAction(action);
}
return;
} else {
this.server.getLogger().debug("Got unexpected normal inventory action with incomplete crafting transaction from " + this.getName() + ", refusing to execute crafting");
this.removeAllWindows(false);
this.sendAllInventories();
this.craftingTransaction = null;
}
} else if (this.enchantTransaction != null) {
if (enchantTransaction.checkForEnchantPart(actions)) {
for (InventoryAction action : actions) {
enchantTransaction.addAction(action);
}
return;
} else {
this.server.getLogger().debug("Got unexpected normal inventory action with incomplete enchanting transaction from " + this.getName() + ", refusing to execute enchant " + transactionPacket.toString());
this.removeAllWindows(false);
this.sendAllInventories();
this.enchantTransaction = null;
}
}

switch (transactionPacket.transactionType) {
Expand Down Expand Up @@ -4532,6 +4556,10 @@ public int addWindow(Inventory inventory, Integer forceId) {
}

public int addWindow(Inventory inventory, Integer forceId, boolean isPermanent) {
return addWindow(inventory, forceId, isPermanent, false);
}

public int addWindow(Inventory inventory, Integer forceId, boolean isPermanent, boolean alwaysOpen) {
if (this.windows.containsKey(inventory)) {
return this.windows.get(inventory);
}
Expand All @@ -4547,13 +4575,17 @@ public int addWindow(Inventory inventory, Integer forceId, boolean isPermanent)
this.permanentWindows.add(cnt);
}

if (inventory.open(this)) {
if (this.spawned && inventory.open(this)) {
return cnt;
} else {
} else if (!alwaysOpen) {
this.removeWindow(inventory);

return -1;
} else {
inventory.getViewers().add(this);
}

return cnt;
}

public Optional<Inventory> getTopWindow() {
Expand Down Expand Up @@ -4582,11 +4614,11 @@ public void sendAllInventories() {
}

protected void addDefaultWindows() {
this.addWindow(this.getInventory(), ContainerIds.INVENTORY, true);
this.getInventory().close(this);
this.addWindow(this.getInventory(), ContainerIds.INVENTORY, true, true);

this.playerUIInventory = new PlayerUIInventory(this);
this.addWindow(this.playerUIInventory, ContainerIds.UI, true);
this.addWindow(this.offhandInventory, ContainerIds.OFFHAND, true);
this.addWindow(this.offhandInventory, ContainerIds.OFFHAND, true, true);

this.craftingGrid = this.playerUIInventory.getCraftingGrid();
this.addWindow(this.craftingGrid, ContainerIds.NONE);
Expand Down Expand Up @@ -4727,7 +4759,7 @@ public static BatchPacket getChunkCacheFromData(int chunkX, int chunkZ, int subC
batchPayload[1] = buf;
byte[] data = Binary.appendBytes(batchPayload);
try {
batch.payload = Network.deflate_raw(data, Server.getInstance().networkCompressionLevel);
batch.payload = Network.deflateRaw(data, Server.getInstance().networkCompressionLevel);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSy
} else {
try {
byte[] data = Binary.appendBytes(payload);
this.broadcastPacketsCallback(Network.deflate_raw(data, this.networkCompressionLevel), targets);
this.broadcastPacketsCallback(Network.deflateRaw(data, this.networkCompressionLevel), targets);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -2287,9 +2287,11 @@ private void registerEntities() {
Entity.registerEntity("Evoker", EntityEvoker.class);
Entity.registerEntity("Ghast", EntityGhast.class);
Entity.registerEntity("Guardian", EntityGuardian.class);
Entity.registerEntity("Hoglin", EntityHoglin.class);
Entity.registerEntity("Husk", EntityHusk.class);
Entity.registerEntity("MagmaCube", EntityMagmaCube.class);
Entity.registerEntity("Phantom", EntityPhantom.class);
Entity.registerEntity("Piglin", EntityPiglin.class);
Entity.registerEntity("Pillager", EntityPillager.class);
Entity.registerEntity("Ravager", EntityRavager.class);
Entity.registerEntity("Shulker", EntityShulker.class);
Expand All @@ -2304,6 +2306,7 @@ private void registerEntities() {
Entity.registerEntity("Wither", EntityWither.class);
Entity.registerEntity("WitherSkeleton", EntityWitherSkeleton.class);
Entity.registerEntity("Zombie", EntityZombie.class);
Entity.registerEntity("Zoglin", EntityZoglin.class);
Entity.registerEntity("ZombiePigman", EntityZombiePigman.class);
Entity.registerEntity("ZombieVillager", EntityZombieVillager.class);
Entity.registerEntity("ZombieVillagerV1", EntityZombieVillagerV1.class);
Expand All @@ -2330,6 +2333,7 @@ private void registerEntities() {
Entity.registerEntity("Sheep", EntitySheep.class);
Entity.registerEntity("SkeletonHorse", EntitySkeletonHorse.class);
Entity.registerEntity("Squid", EntitySquid.class);
Entity.registerEntity("Strider", EntityStrider.class);
Entity.registerEntity("TropicalFish", EntityTropicalFish.class);
Entity.registerEntity("Turtle", EntityTurtle.class);
Entity.registerEntity("Villager", EntityVillager.class);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/cn/nukkit/entity/mob/EntityHoglin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.nukkit.entity.mob;

import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;

/**
* @author Erik Miller | EinBexiii
*/
public class EntityHoglin extends EntityMob {

public final static int NETWORK_ID = 124;

@Override
public int getNetworkId() {
return NETWORK_ID;
}

public EntityHoglin(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
}

@Override
protected void initEntity() {
super.initEntity();
this.setMaxHealth(40);
}

@Override
public float getWidth() {
return 0.9f;
}

@Override
public float getHeight() {
return 0.9f;
}

@Override
public String getName() {
return "Hoglin";
}
}
Loading

0 comments on commit 69c992f

Please sign in to comment.