Skip to content

Commit

Permalink
Implement better inventory management
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 2, 2024
1 parent 5f7fe9c commit 44e34c4
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.monster.Shulker;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.windcharge.AbstractWindCharge;
import net.minecraft.world.entity.vehicle.AbstractBoat;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
Expand Down Expand Up @@ -83,6 +85,12 @@ public static JsonObject generateEntity(EntityType<?> entityType) {
if (defaultEntity instanceof LivingEntity le && le.getAttributes().hasAttribute(Attributes.FOLLOW_RANGE)) {
entityDesc.addProperty("defaultFollowRange", le.getAttributeValue(Attributes.FOLLOW_RANGE));
}
if (defaultEntity instanceof Player) {
entityDesc.addProperty("playerEntity", true);
}
if (defaultEntity instanceof LivingEntity) {
entityDesc.addProperty("livingEntity", true);
}
if (defaultEntity instanceof AbstractBoat) {
entityDesc.addProperty("boatEntity", true);
}
Expand All @@ -92,6 +100,9 @@ public static JsonObject generateEntity(EntityType<?> entityType) {
if (defaultEntity instanceof AbstractWindCharge) {
entityDesc.addProperty("windChargeEntity", true);
}
if (defaultEntity instanceof Shulker) {
entityDesc.addProperty("shulkerEntity", true);
}

var inheritedClasses = new JsonArray();
Class<?> clazz = defaultEntity.getClass();
Expand Down
3 changes: 3 additions & 0 deletions data-generator/src/main/resources/templates/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public record EntityType(
boolean fireImmune,
boolean attackable,
double defaultFollowRange,
boolean playerEntity,
boolean livingEntity,
boolean boatEntity,
boolean minecartEntity,
boolean windChargeEntity,
boolean shulkerEntity,
List<String> inheritedClasses,
String defaultEntityMetadata) implements RegistryValue<EntityType> {
public static final Registry<EntityType> REGISTRY = new Registry<>(RegistryKeys.ENTITY_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public record EntityType(
boolean fireImmune,
boolean attackable,
double defaultFollowRange,
boolean playerEntity,
boolean livingEntity,
boolean boatEntity,
boolean minecartEntity,
boolean windChargeEntity,
boolean shulkerEntity,
List<String> inheritedClasses,
String defaultEntityMetadata) implements RegistryValue<EntityType> {
public static final Registry<EntityType> REGISTRY = new Registry<>(RegistryKeys.ENTITY_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/
package com.soulfiremc.server.data;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum EquipmentSlot {
MAINHAND(Type.HAND),
Expand Down
22 changes: 11 additions & 11 deletions server/src/main/java/com/soulfiremc/server/plugins/POVServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import com.soulfiremc.server.protocol.bot.container.ContainerSlot;
import com.soulfiremc.server.protocol.bot.model.ChunkKey;
import com.soulfiremc.server.protocol.bot.state.LevelHeightAccessor;
import com.soulfiremc.server.protocol.bot.state.entity.Entity;
import com.soulfiremc.server.protocol.bot.state.entity.ExperienceOrbEntity;
import com.soulfiremc.server.protocol.bot.state.entity.LocalPlayer;
import com.soulfiremc.server.protocol.bot.state.entity.RawEntity;
import com.soulfiremc.server.protocol.bot.state.registry.SFChatType;
import com.soulfiremc.server.settings.BotSettings;
import com.soulfiremc.server.settings.lib.SettingsObject;
Expand Down Expand Up @@ -659,7 +659,7 @@ private static void syncBotAndUser(BotConnection botConnection, Session clientSe
if (botConnection.inventoryManager() != null) {
clientSession.send(
new ClientboundSetHeldSlotPacket(
botConnection.inventoryManager().heldItemSlot()));
botConnection.inventoryManager().playerInventory().selected));
var stateIndex = 0;
for (var container :
botConnection.inventoryManager().containerData().values()) {
Expand Down Expand Up @@ -710,7 +710,15 @@ private static void syncBotAndUser(BotConnection botConnection, Session clientSe
localPlayer.showReducedDebug()
? EntityEvent.PLAYER_ENABLE_REDUCED_DEBUG
: EntityEvent.PLAYER_DISABLE_REDUCED_DEBUG));
} else if (entity instanceof RawEntity rawEntity) {
} else if (entity instanceof ExperienceOrbEntity experienceOrbEntity) {
clientSession.send(
new ClientboundAddExperienceOrbPacket(
entity.entityId(),
entity.x(),
entity.y(),
entity.z(),
experienceOrbEntity.expValue()));
} else if (entity instanceof Entity rawEntity) {
clientSession.send(
new ClientboundAddEntityPacket(
entity.entityId(),
Expand All @@ -726,14 +734,6 @@ private static void syncBotAndUser(BotConnection botConnection, Session clientSe
entity.deltaMovement().getX(),
entity.deltaMovement().getY(),
entity.deltaMovement().getZ()));
} else if (entity instanceof ExperienceOrbEntity experienceOrbEntity) {
clientSession.send(
new ClientboundAddExperienceOrbPacket(
entity.entityId(),
entity.x(),
entity.y(),
entity.z(),
experienceOrbEntity.expValue()));
}

for (var effect : entity.effectState().effects().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
import io.netty.handler.traffic.GlobalTrafficShapingHandler;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
import org.geysermc.mcprotocollib.protocol.data.ProtocolState;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundClientTickEndPacket;
import org.slf4j.Logger;
import org.slf4j.MDC;
Expand Down Expand Up @@ -255,4 +257,26 @@ public void joinServerId(String serverId) {
public void sendPacket(Packet packet) {
session.send(packet);
}

public GameMode getEntityGameMode(UUID uuid) {
if (uuid.equals(dataManager.localPlayer().uuid())) {
return dataManager.gameMode();
}

var profile = dataManager.playerListState().entries().get(uuid);
if (profile == null) {
return GameMode.SURVIVAL;
}

return profile.getGameMode();
}

public GameProfile getEntityProfile(UUID uuid) {
var profile = dataManager.playerListState().entries().get(uuid);
if (profile == null) {
return null;
}

return profile.getProfile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import com.soulfiremc.server.protocol.bot.container.WindowContainer;
import com.soulfiremc.server.protocol.bot.model.*;
import com.soulfiremc.server.protocol.bot.state.*;
import com.soulfiremc.server.protocol.bot.state.entity.EntityFactory;
import com.soulfiremc.server.protocol.bot.state.entity.ExperienceOrbEntity;
import com.soulfiremc.server.protocol.bot.state.entity.LivingEntity;
import com.soulfiremc.server.protocol.bot.state.entity.LocalPlayer;
import com.soulfiremc.server.protocol.bot.state.entity.RawEntity;
import com.soulfiremc.server.protocol.bot.state.registry.Biome;
import com.soulfiremc.server.protocol.bot.state.registry.DimensionType;
import com.soulfiremc.server.protocol.bot.state.registry.SFChatType;
Expand Down Expand Up @@ -256,6 +257,8 @@ public void onJoin(ClientboundLoginPacket packet) {
new LocalPlayer(connection, currentLevel(), botProfile);
localPlayer.entityId(packet.getEntityId());
localPlayer.showReducedDebug(packet.isReducedDebugInfo());
connection.inventoryManager().setContainer(0, localPlayer.inventory());

entityTrackerState.addEntity(localPlayer);
}

Expand Down Expand Up @@ -607,7 +610,22 @@ public void onSetCursor(ClientboundSetCursorItemPacket packet) {

@EventHandler
public void onSetSlot(ClientboundSetHeldSlotPacket packet) {
connection.inventoryManager().heldItemSlot(packet.getSlot());
localPlayer.inventory().selected = packet.getSlot();
}

@EventHandler
public void onSetEquipment(ClientboundSetEquipmentPacket packet) {
var entity = entityTrackerState.getEntity(packet.getEntityId());
if (entity == null) {
log.debug("Received equipment update for unknown entity {}", packet.getEntityId());
return;
}

if (entity instanceof LivingEntity le) {
for (var entry : packet.getEquipment()) {
le.setItemSlot(EquipmentSlot.fromMCPl(entry.getSlot()), SFItemStack.from(entry.getItem()));
}
}
}

@EventHandler
Expand Down Expand Up @@ -803,10 +821,11 @@ public void onBorderWarningBlocks(ClientboundSetBorderWarningDistancePacket pack

@EventHandler
public void onEntitySpawn(ClientboundAddEntityPacket packet) {
var entityState =
new RawEntity(
EntityType.REGISTRY.getById(packet.getType().ordinal()),
currentLevel());
var entityState = EntityFactory.createEntity(
connection,
EntityType.REGISTRY.getById(packet.getType().ordinal()),
currentLevel(),
packet.getUuid());
entityState.fromAddEntityPacket(packet);

entityTrackerState.addEntity(entityState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@
@Data
@RequiredArgsConstructor
public class InventoryManager {
private final PlayerInventoryContainer playerInventory = new PlayerInventoryContainer(this);
private final Int2ObjectMap<Container> containerData =
new Int2ObjectOpenHashMap<>(Map.of(0, playerInventory));
private final Int2ObjectMap<Container> containerData = new Int2ObjectOpenHashMap<>();
private final Map<EquipmentSlot, SFItemStack> lastInEquipment = new EnumMap<>(EquipmentSlot.class);
private final ReentrantLock inventoryControlLock = new ReentrantLock();
@ToString.Exclude
private final BotConnection connection;
private Container currentContainer;
private int heldItemSlot = 0;
private int lastStateId = 0;
private SFItemStack cursorItem;

Expand Down Expand Up @@ -77,7 +74,7 @@ public void setContainer(int containerId, Container container) {
}

private void sendHeldItemChange() {
connection.sendPacket(new ServerboundSetCarriedItemPacket(heldItemSlot));
connection.sendPacket(new ServerboundSetCarriedItemPacket(playerInventory().selected));
}

public void closeInventory() {
Expand All @@ -90,15 +87,19 @@ public void closeInventory() {
}

public boolean lookingAtForeignContainer() {
return currentContainer != null && currentContainer != playerInventory;
return currentContainer != null && currentContainer != playerInventory();
}

public void openPlayerInventory() {
currentContainer = playerInventory();
}

public PlayerInventoryContainer playerInventory() {
return connection.dataManager().localPlayer().inventory();
}

public void changeHeldItem(int slot) {
heldItemSlot = slot;
playerInventory().selected = slot;
sendHeldItemChange();
}

Expand Down Expand Up @@ -165,7 +166,7 @@ public void applyItemAttributes() {
}

private void applyIfMatches(EquipmentSlot equipmentSlot) {
var item = playerInventory.getEquipmentSlotItem(equipmentSlot);
var item = playerInventory().getEquipmentSlotItem(equipmentSlot);
var previousItem = lastInEquipment.get(equipmentSlot);
boolean hasChanged;
if (previousItem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

@Getter
public class PlayerInventoryContainer extends Container {
private final InventoryManager inventoryManager;
private final ContainerSlot[] mainInventory = getSlots(9, 35);
private final ContainerSlot[] hotbar = getSlots(36, 44);

Expand All @@ -39,10 +38,10 @@ public class PlayerInventoryContainer extends Container {
private final ContainerSlot[] armor = getSlots(5, 8);
@Getter
private final ContainerSlot[] craftingGrid = getSlots(1, 4);
public int selected;

public PlayerInventoryContainer(InventoryManager inventoryManager) {
public PlayerInventoryContainer() {
super(46, 0);
this.inventoryManager = inventoryManager;
}

public Optional<SFItemStack> getEquipmentSlotItem(EquipmentSlot slot) {
Expand All @@ -66,7 +65,7 @@ public void setEquipmentSlotItem(EquipmentSlot slot, @Nullable SFItemStack item)
}

public ContainerSlot getHeldItem() {
return hotbarSlot(inventoryManager.heldItemSlot());
return hotbarSlot(selected);
}

public boolean isHeldItem(ContainerSlot slot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.protocol.bot.state.Level;

public class AbstractBoat extends VehicleEntity {
public AbstractBoat(EntityType entityType, Level level) {
super(entityType, level);
}

@Override
public boolean canCollideWith(Entity entity) {
return entity.canBeCollidedWith() || entity.isPushable();
}

@Override
public boolean canBeCollidedWith() {
return true;
}

@Override
public boolean isPushable() {
return true;
}

@Override
public void push(Entity entity) {
if (entity instanceof AbstractBoat) {
if (entity.getBoundingBox().minY < this.getBoundingBox().maxY) {
super.push(entity);
}
} else if (entity.getBoundingBox().minY <= this.getBoundingBox().minY) {
super.push(entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public AbstractClientPlayer(BotConnection connection, Level level, GameProfile g

@Override
public boolean isSpectator() {
return connection.dataManager().gameMode() == GameMode.SPECTATOR;
return connection.getEntityGameMode(uuid) == GameMode.SPECTATOR;
}

@Override
public boolean isCreative() {
return connection.dataManager().gameMode() == GameMode.CREATIVE;
return connection.getEntityGameMode(uuid) == GameMode.CREATIVE;
}
}
Loading

0 comments on commit 44e34c4

Please sign in to comment.