Inventories!

This commit is contained in:
LOOHP
2022-12-09 16:41:59 +00:00
parent 949d2f34d7
commit 35c6b748c9
29 changed files with 1685 additions and 83 deletions
@@ -20,6 +20,8 @@
package com.loohp.limbo.network;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.entity.EntityEquipment;
import com.loohp.limbo.events.inventory.AnvilRenameInputEvent;
import com.loohp.limbo.events.inventory.InventoryCloseEvent;
import com.loohp.limbo.events.inventory.InventoryCreativeEvent;
import com.loohp.limbo.events.player.PlayerInteractEvent;
@@ -30,10 +32,13 @@ import com.loohp.limbo.events.player.PlayerQuitEvent;
import com.loohp.limbo.events.player.PlayerResourcePackStatusEvent;
import com.loohp.limbo.events.player.PlayerSelectedSlotChangeEvent;
import com.loohp.limbo.events.player.PlayerSpawnEvent;
import com.loohp.limbo.events.player.PlayerSwapHandItemsEvent;
import com.loohp.limbo.events.player.PluginMessageEvent;
import com.loohp.limbo.events.status.StatusPingEvent;
import com.loohp.limbo.file.ServerProperties;
import com.loohp.limbo.inventory.AnvilInventory;
import com.loohp.limbo.inventory.Inventory;
import com.loohp.limbo.inventory.ItemStack;
import com.loohp.limbo.location.Location;
import com.loohp.limbo.network.protocol.packets.Packet;
import com.loohp.limbo.network.protocol.packets.PacketHandshakingIn;
@@ -44,11 +49,14 @@ import com.loohp.limbo.network.protocol.packets.PacketLoginOutDisconnect;
import com.loohp.limbo.network.protocol.packets.PacketLoginOutLoginSuccess;
import com.loohp.limbo.network.protocol.packets.PacketLoginOutPluginMessaging;
import com.loohp.limbo.network.protocol.packets.PacketOut;
import com.loohp.limbo.network.protocol.packets.PacketPlayInBlockDig;
import com.loohp.limbo.network.protocol.packets.PacketPlayInBlockPlace;
import com.loohp.limbo.network.protocol.packets.PacketPlayInChat;
import com.loohp.limbo.network.protocol.packets.PacketPlayInCloseWindow;
import com.loohp.limbo.network.protocol.packets.PacketPlayInHeldItemChange;
import com.loohp.limbo.network.protocol.packets.PacketPlayInItemName;
import com.loohp.limbo.network.protocol.packets.PacketPlayInKeepAlive;
import com.loohp.limbo.network.protocol.packets.PacketPlayInPickItem;
import com.loohp.limbo.network.protocol.packets.PacketPlayInPluginMessaging;
import com.loohp.limbo.network.protocol.packets.PacketPlayInPosition;
import com.loohp.limbo.network.protocol.packets.PacketPlayInPositionAndLook;
@@ -85,6 +93,7 @@ import com.loohp.limbo.network.protocol.packets.PacketStatusOutResponse;
import com.loohp.limbo.network.protocol.packets.ServerboundChatCommandPacket;
import com.loohp.limbo.player.Player;
import com.loohp.limbo.player.PlayerInteractManager;
import com.loohp.limbo.player.PlayerInventory;
import com.loohp.limbo.utils.BungeecordAdventureConversionUtils;
import com.loohp.limbo.utils.CheckedBiConsumer;
import com.loohp.limbo.utils.CustomStringUtils;
@@ -92,6 +101,7 @@ import com.loohp.limbo.utils.DataTypeIO;
import com.loohp.limbo.utils.DeclareCommands;
import com.loohp.limbo.utils.ForwardingUtils;
import com.loohp.limbo.utils.GameMode;
import com.loohp.limbo.utils.InventoryClickUtils;
import com.loohp.limbo.utils.MojangAPIUtils;
import com.loohp.limbo.utils.MojangAPIUtils.SkinResponse;
import com.loohp.limbo.world.BlockPosition;
@@ -99,6 +109,7 @@ import com.loohp.limbo.world.BlockState;
import com.loohp.limbo.world.World;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
@@ -124,7 +135,6 @@ import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
@@ -668,9 +678,9 @@ public class ClientConnection extends Thread {
sendPacket(response);
}
};
System.out.println("Waiting");
PacketIn packetIn = channel.readPacket();
System.out.println("Received " + packetIn.getClass());
if (packetIn instanceof PacketPlayInPositionAndLook) {
PacketPlayInPositionAndLook pos = (PacketPlayInPositionAndLook) packetIn;
Location from = player.getLocation();
@@ -757,33 +767,80 @@ public class ClientConnection extends Thread {
Limbo.getInstance().getEventsManager().callEvent(new PlayerInteractEvent(player, PlayerInteractEvent.Action.RIGHT_CLICK_AIR, player.getEquipment().getItem(packet.getHand()), block, packet.getBlockHit().getDirection(), packet.getHand()));
} else if (packetIn instanceof PacketPlayInSetCreativeSlot) {
PacketPlayInSetCreativeSlot packet = (PacketPlayInSetCreativeSlot) packetIn;
InventoryCreativeEvent event = Limbo.getInstance().getEventsManager().callEvent(new InventoryCreativeEvent(player, player.getInventoryView(), player.getInventory(), player.getInventory().getUnsafe().b().applyAsInt(packet.getSlotNumber()), packet.getItemStack()));
InventoryCreativeEvent event = Limbo.getInstance().getEventsManager().callEvent(new InventoryCreativeEvent(player.getInventoryView(), player.getInventory().getUnsafe().b().applyAsInt(packet.getSlotNumber()), packet.getItemStack()));
if (event.isCancelled()) {
player.updateInventory();
} else {
player.getInventory().getUnsafe().b(packet.getSlotNumber(), event.getNewItem());
player.getInventory().setItem(event.getSlot(), event.getNewItem());
}
} else if (packetIn instanceof PacketPlayInWindowClick) {
PacketPlayInWindowClick packet = (PacketPlayInWindowClick) packetIn;
/*
InventoryCreativeEvent event = Limbo.getInstance().getEventsManager().callEvent(new InventoryCreativeEvent(player, player.getInventoryView(), player.getInventory(), player.getInventory().getUnsafe().b().applyAsInt(packet.getSlotNumber()), packet.getItemStack()));
if (event.isCancelled()) {
player.updateInventory();
} else {
player.getInventory().getUnsafe().b(packet.getSlotNumber(), event.getNewItem());
try {
InventoryClickUtils.handle(player, packet);
} catch (Throwable e) {
e.printStackTrace();
}
*/
} else if (packetIn instanceof PacketPlayInCloseWindow) {
PacketPlayInCloseWindow packet = (PacketPlayInCloseWindow) packetIn;
Inventory inventory = player.getInventoryView().getTopInventory();
if (inventory != null) {
Integer id = inventory.getUnsafe().c().get(player);
if (id != null) {
Limbo.getInstance().getEventsManager().callEvent(new InventoryCloseEvent(player, player.getInventoryView()));
player.getInventoryView().getUnsafe().a(null);
Limbo.getInstance().getEventsManager().callEvent(new InventoryCloseEvent(player.getInventoryView()));
player.getInventoryView().getUnsafe().a(null, null);
inventory.getUnsafe().c().remove(player);
}
}
} else if (packetIn instanceof PacketPlayInBlockDig) {
PacketPlayInBlockDig packet = (PacketPlayInBlockDig) packetIn;
//noinspection SwitchStatementWithTooFewBranches
switch (packet.getAction()) {
case SWAP_ITEM_WITH_OFFHAND: {
EntityEquipment equipment = player.getEquipment();
PlayerSwapHandItemsEvent event = Limbo.getInstance().getEventsManager().callEvent(new PlayerSwapHandItemsEvent(player, equipment.getItemInOffHand(), equipment.getItemInMainHand()));
if (!event.isCancelled()) {
equipment.setItemInMainHand(event.getMainHandItem());
equipment.setItemInOffHand(event.getOffHandItem());
}
break;
}
}
} else if (packetIn instanceof PacketPlayInPickItem) {
PacketPlayInPickItem packet = (PacketPlayInPickItem) packetIn;
PlayerInventory inventory = player.getInventory();
int slot = inventory.getUnsafe().b().applyAsInt(packet.getSlot());
int i = player.getSelectedSlot();
byte selectedSlot = -1;
boolean firstRun = true;
while (selectedSlot < 0 || (!firstRun && i == player.getSelectedSlot())) {
ItemStack itemStack = inventory.getItem(i);
if (itemStack == null) {
selectedSlot = (byte) i;
break;
}
if (++i >= 9) {
i = 0;
}
}
if (selectedSlot < 0) {
selectedSlot = player.getSelectedSlot();
}
ItemStack leavingHotbar = inventory.getItem(selectedSlot);
inventory.setItem(selectedSlot, inventory.getItem(slot));
inventory.setItem(slot, leavingHotbar);
player.setSelectedSlot(selectedSlot);
} else if (packetIn instanceof PacketPlayInItemName) {
PacketPlayInItemName packet = (PacketPlayInItemName) packetIn;
if (player.getInventoryView().getTopInventory() instanceof AnvilInventory) {
AnvilRenameInputEvent event = Limbo.getInstance().getEventsManager().callEvent(new AnvilRenameInputEvent(player.getInventoryView(), packet.getName()));
if (!event.isCancelled()) {
AnvilInventory anvilInventory = (AnvilInventory) player.getInventoryView().getTopInventory();
ItemStack result = anvilInventory.getItem(2);
if (result != null) {
result.displayName(LegacyComponentSerializer.legacySection().deserialize(event.getInput()));
}
}
}
}
} catch (Exception e) {
break;
@@ -0,0 +1,74 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.location.BlockFace;
import com.loohp.limbo.utils.DataTypeIO;
import com.loohp.limbo.world.BlockPosition;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketPlayInBlockDig extends PacketIn {
public enum PlayerDigType {
START_DESTROY_BLOCK,
ABORT_DESTROY_BLOCK,
STOP_DESTROY_BLOCK,
DROP_ALL_ITEMS,
DROP_ITEM,
RELEASE_USE_ITEM,
SWAP_ITEM_WITH_OFFHAND;
}
private PlayerDigType action;
private BlockPosition pos;
private BlockFace direction;
private int sequence;
public PacketPlayInBlockDig(PlayerDigType action, BlockPosition pos, BlockFace direction, int sequence) {
this.action = action;
this.pos = pos;
this.direction = direction;
this.sequence = sequence;
}
public PacketPlayInBlockDig(DataInputStream in) throws IOException {
this(PlayerDigType.values()[DataTypeIO.readVarInt(in)], DataTypeIO.readBlockPosition(in), BlockFace.values()[in.readByte()], DataTypeIO.readVarInt(in));
}
public BlockPosition getPos() {
return pos;
}
public BlockFace getDirection() {
return direction;
}
public PlayerDigType getAction() {
return action;
}
public int getSequence() {
return sequence;
}
}
@@ -0,0 +1,43 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.utils.DataTypeIO;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class PacketPlayInItemName extends PacketIn {
private String name;
public PacketPlayInItemName(String name) {
this.name = name;
}
public PacketPlayInItemName(DataInputStream in) throws IOException {
this(DataTypeIO.readString(in, StandardCharsets.UTF_8));
}
public String getName() {
return name;
}
}
@@ -0,0 +1,42 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.utils.DataTypeIO;
import java.io.DataInputStream;
import java.io.IOException;
public class PacketPlayInPickItem extends PacketIn {
private int slot;
public PacketPlayInPickItem(int slot) {
this.slot = slot;
}
public PacketPlayInPickItem(DataInputStream in) throws IOException {
this(DataTypeIO.readVarInt(in));
}
public int getSlot() {
return slot;
}
}
@@ -31,7 +31,7 @@ public class PacketPlayOutPlayerAbilities extends PacketOut {
ALLOW_FLYING(0x04),
CREATIVE(0x08);
int bitvalue;
private final int bitvalue;
PlayerAbilityFlags(int bitvalue) {
this.bitvalue = bitvalue;
@@ -38,7 +38,7 @@ public class PacketPlayOutSetSlot extends PacketOut {
this.containerId = containerId;
this.stateId = stateId;
this.slot = slot;
this.itemStack = itemStack;
this.itemStack = itemStack == null ? ItemStack.AIR : itemStack;
}
public int getContainerId() {