Skip to content

Commit

Permalink
make completeEntityToLua returns effect of the entity
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Apr 27, 2024
1 parent dea2e60 commit 6d1c2d3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.items.wrapper.InvWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -95,7 +96,7 @@ public ItemStack getToolInMainHand() {

@Override
public ItemStack storeItem(ItemStack stored) {
return InventoryUtil.storeItems(stored, turtle.getItemHandler(), turtle.getSelectedSlot());
return InventoryUtil.storeItems(stored, new InvWrapper(turtle.getInventory()), turtle.getSelectedSlot());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.common.entity.TurtleSeatEntity;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -160,7 +161,7 @@ public boolean hasRider() {
public MethodResult getRider() {
Entity entity = getRidingEntity();
if (entity == null) {
return MethodResult.of(null);
return MethodResult.of(null, "No entity is riding");
}
return MethodResult.of(LuaConverter.completeEntityToLua(entity));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Mod.EventBusSubscriber(modid = AdvancedPeripherals.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class APEntities {

public static final RegistryObject<EntityType<TurtleSeatEntity>> TURTLE_SEAT = Registration.ENTITIES.register("turtle_seat",
public static final RegistryObject<EntityType<TurtleSeatEntity>> TURTLE_SEAT = APRegistration.ENTITIES.register("turtle_seat",
() -> EntityType.Builder.<TurtleSeatEntity>of(TurtleSeatEntity::new, MobCategory.MISC)
.sized(0.1F, 0.1F)
.clientTrackingRange(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public static void register() {
APEntities.register();
APContainerTypes.register();
APVillagers.register();
Blocks.register();
BlockEntityTypes.register();
Items.register();
ContainerTypes.register();
Villagers.register();
CCRegistration.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import net.minecraft.tags.TagKey;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.npc.InventoryCarrier;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -21,8 +24,8 @@
import net.minecraftforge.common.IForgeShearable;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;
import org.jetbrains.annotations.NotNull;

import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -42,7 +45,15 @@ public static Map<String, Object> entityToLua(Entity entity) {
data.put("isGlowing", entity.isCurrentlyGlowing());
data.put("isInWall", entity.isInWall());
if (entity instanceof InventoryCarrier carrier) {
Map<Integer, Object> invMap = new HashMap<>();
SimpleContainer inv = carrier.getInventory();
for (int slot = 0; slot < inv.getContainerSize(); slot++) {
ItemStack item = inv.getItem(slot);
if (!item.isEmpty()) {
invMap.put(slot, itemStackToObject(item));
}
}
data.put("inventory", invMap);
}
return data;
}
Expand All @@ -53,6 +64,11 @@ public static Map<String, Object> livingEntityToLua(LivingEntity entity) {
data.put("health", entity.getHealth());
data.put("maxHealth", entity.getMaxHealth());
data.put("lastDamageSource", entity.getLastDamageSource() == null ? null : entity.getLastDamageSource().toString());
Map<String, Object> effMap = new HashMap<>();
entity.getActiveEffectsMap().forEach((key, value) -> {
effMap.put(key.getDescriptionId(), effectToObject(value));
});
data.put("effects", effMap);
return data;
}

Expand All @@ -67,19 +83,36 @@ public static Map<String, Object> animalToLua(Animal animal, ItemStack itemInHan
}

public static Map<String, Object> playerToLua(Player player) {
Map<String, Object> data = livingEntityToLua(animal);
Map<String, Object> data = livingEntityToLua(player);
data.put("score", player.getScore());
data.put("luck", player.getLuck());
Map<Integer, Object> invMap = new HashMap<>();
Inventory inv = player.getInventory();
for (int slot = 0; slot < inv.getContainerSize(); slot++) {
ItemStack item = inv.getItem(slot);
if (!item.isEmpty()) {
invMap.put(slot, itemStackToObject(item));
}
}
data.put("inventory", invMap);
return data;
}

public static Map<String, Object> completeEntityToLua(Entity entity) {
return completeEntityToLua(entity, ItemStack.EMPTY);
}

public static Map<String, Object> completeEntityToLua(Entity entity, ItemStack itemInHand) {
if (entity instanceof Player player) return playerToLua(player);
if (entity instanceof Animal animal) return animalToLua(animal, itemInHand);
if (entity instanceof LivingEntity livingEntity) return livingEntityToLua(livingEntity);
return entityToLua(entity);
}

public static Map<String, Object> completeEntityWithPositionToLua(Entity entity, BlockPos pos) {
return completeEntityWithPositionToLua(entity, ItemStack.EMPTY, pos);
}

public static Map<String, Object> completeEntityWithPositionToLua(Entity entity, ItemStack itemInHand, BlockPos pos) {
Map<String, Object> data = completeEntityToLua(entity, itemInHand);
data.put("x", entity.getX() - pos.getX());
Expand Down Expand Up @@ -202,4 +235,12 @@ public static BlockPos convertToBlockPos(BlockPos center, Map<?, ?> table) throw
BlockPos relative = convertToBlockPos(table);
return new BlockPos(center.getX() + relative.getX(), center.getY() + relative.getY(), center.getZ() + relative.getZ());
}

public static Object effectToObject(MobEffectInstance effect) {
Map<String, Object> map = new HashMap<>();
map.put("name", effect.getDescriptionId());
map.put("duration", effect.getDuration());
map.put("amplifier", effect.getAmplifier());
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.InvWrapper;

import java.util.WeakHashMap;
import java.util.function.Function;
Expand Down Expand Up @@ -51,22 +53,22 @@ public static void load(APFakePlayer player, ITurtleAccess turtle) {
playerInventory.selected = 0;

// Copy primary items into player inventory and empty the rest
IItemHandler turtleInventory = turtle.getItemHandler();
int size = turtleInventory.getSlots();
Container turtleInventory = turtle.getInventory();
int size = turtleInventory.getContainerSize();
int largerSize = playerInventory.getContainerSize();
playerInventory.selected = turtle.getSelectedSlot();
for (int i = 0; i < size; i++) {
playerInventory.setItem(i, turtleInventory.getStackInSlot(i));
playerInventory.setItem(i, turtleInventory.getItem(i));
}
for (int i = size; i < largerSize; i++) {
playerInventory.setItem(i, ItemStack.EMPTY);
}

// Add properties
ItemStack activeStack = player.getItemInHand(InteractionHand.MAIN_HAND);
if (!activeStack.isEmpty())
if (!activeStack.isEmpty()) {
player.getAttributes().addTransientAttributeModifiers(activeStack.getAttributeModifiers(EquipmentSlot.MAINHAND));

}
}

public static void unload(APFakePlayer player, ITurtleAccess turtle) {
Expand All @@ -80,19 +82,19 @@ public static void unload(APFakePlayer player, ITurtleAccess turtle) {
}

// Copy primary items into turtle inventory and then insert/drop the rest
IItemHandlerModifiable turtleInventory = turtle.getItemHandler();
int size = turtleInventory.getSlots();
Container turtleInventory = turtle.getInventory();
int size = turtleInventory.getContainerSize();
int largerSize = playerInventory.getContainerSize();
playerInventory.selected = turtle.getSelectedSlot();
for (int i = 0; i < size; i++) {
turtleInventory.setStackInSlot(i, playerInventory.getItem(i));
turtleInventory.setItem(i, playerInventory.getItem(i));
playerInventory.setItem(i, ItemStack.EMPTY);
}

for (int i = size; i < largerSize; i++) {
ItemStack remaining = playerInventory.getItem(i);
if (!remaining.isEmpty()) {
remaining = ItemHandlerHelper.insertItem(turtleInventory, remaining, false);
remaining = ItemHandlerHelper.insertItem(new InvWrapper(turtleInventory), remaining, false);
if (!remaining.isEmpty()) {
BlockPos position = turtle.getPosition();
WorldUtil.dropItemStack(remaining, turtle.getLevel(), position, turtle.getDirection().getOpposite());
Expand Down

0 comments on commit 6d1c2d3

Please sign in to comment.