Skip to content

Extensible handlers for entities #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: version/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions src/api/java/com/ldtteam/structurize/api/util/ItemStackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,57 +147,6 @@ public static int getSize(final ItemStack stack)
return stack.getCount();
}

/**
* Get the list of required resources for entities.
*
* @param entity the entity object.
* @param pos the placer pos..
* @return a list of stacks.
*/
public static List<ItemStack> getListOfStackForEntity(final Entity entity, final BlockPos pos)
{
if (entity != null)
{
final List<ItemStack> request = new ArrayList<>();
if (entity instanceof ItemFrame)
{
final ItemStack stack = ((ItemFrame) entity).getItem();
if (!ItemStackUtils.isEmpty(stack))
{
stack.setCount(1);
request.add(stack);
}
request.add(new ItemStack(Items.ITEM_FRAME, 1));
}
else if (entity instanceof ArmorStand)
{
request.add(entity.getPickedResult(new HitResult(Vec3.atLowerCornerOf(pos)) {
@Override
public Type getType()
{
return Type.ENTITY;
}
}));
entity.getArmorSlots().forEach(request::add);
entity.getHandSlots().forEach(request::add);
}
else if (entity instanceof ContainerEntity containerEntity)
{
request.add(entity.getPickedResult(new HitResult(Vec3.atLowerCornerOf(pos)) {
@Override
public Type getType()
{
return Type.ENTITY;
}
}));
request.addAll(containerEntity.getItemStacks());
}

return request.stream().filter(stack -> !stack.isEmpty()).collect(Collectors.toList());
}
return Collections.emptyList();
}

/**
* Method to compare to stacks, ignoring their stacksize.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public final class WindowConstants
public static final String RESOURCE_QUANTITY_MISSING = "resourceQuantity";
public static final String RESOURCE_ICON_FILL = "resourceIconFill";
public static final String RESOURCE_ICON_MAIN = "resourceIconMain";
public static final String RESOURCE_STATUS = "resourceStatus";

/**
* Name of the input range field.
Expand Down
63 changes: 43 additions & 20 deletions src/main/java/com/ldtteam/structurize/client/gui/WindowScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.ldtteam.structurize.api.util.constant.Constants;
import com.ldtteam.structurize.blockentities.interfaces.IBlueprintDataProviderBE;
import com.ldtteam.structurize.network.messages.*;
import com.ldtteam.structurize.placement.handlers.entity.EntityHandlers;
import com.ldtteam.structurize.placement.handlers.entity.IEntityHandler;
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
import com.ldtteam.structurize.storage.rendering.RenderingCache;
Expand All @@ -21,6 +23,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
Expand All @@ -35,7 +38,9 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

import static com.ldtteam.structurize.api.util.constant.Constants.MOD_ID;
import static com.ldtteam.structurize.api.util.constant.WindowConstants.*;

/**
Expand All @@ -51,6 +56,9 @@ public class WindowScan extends AbstractWindowSkeleton
/** chest warning message */
private static final String CHEST_WARNING = "chestwarning";

private static final ResourceLocation STATUS_HANDLED = new ResourceLocation(MOD_ID, "textures/gui/buildtool/green_icon.png");
private static final ResourceLocation STATUS_UNHANDLED = new ResourceLocation(MOD_ID, "textures/gui/buildtool/red_icon.png");

/**
* Id of clicking enter.
*/
Expand All @@ -64,7 +72,7 @@ public class WindowScan extends AbstractWindowSkeleton
/**
* Contains all entities needed for a certain build.
*/
private final Object2IntMap<EntityType> entities = new Object2IntOpenHashMap<>();
private final Map<EntityType<?>, List<Entity>> entities = new HashMap<>();

/**
* White color.
Expand Down Expand Up @@ -118,7 +126,7 @@ public class WindowScan extends AbstractWindowSkeleton
*/
public WindowScan(@NotNull final ScanToolData data)
{
super(Constants.MOD_ID + BUILDING_NAME_RESOURCE_SUFFIX);
super(MOD_ID + BUILDING_NAME_RESOURCE_SUFFIX);
this.data = data;
registerButton(BUTTON_CONFIRM, this::confirmClicked);
registerButton(BUTTON_CANCEL, this::discardClicked);
Expand Down Expand Up @@ -214,7 +222,7 @@ private void removeEntity(final Button button)
final int row = entityList.getListElementIndexByPane(button);
final EntityType entity = new ArrayList<>(entities.keySet()).get(row);
Network.getNetwork().sendToServer(new RemoveEntityMessage(new BlockPos(x1, y1, z1), new BlockPos(x2, y2, z2), EntityType.getKey(entity)));
entities.removeInt(entity);
entities.remove(entity);
updateEntitylist();
}

Expand Down Expand Up @@ -444,7 +452,7 @@ private void updateResources()

final ScanToolData.Slot slot = data.getCurrentSlotData();

final List<Entity> list = world.getEntitiesOfClass(Entity.class, new AABB(slot.getBox().getPos1(), slot.getBox().getPos2()));
final List<Entity> list = world.getEntitiesOfClass(Entity.class, slot.getBox().getAABB());

for (final Entity entity : list)
{
Expand All @@ -455,7 +463,7 @@ private void updateResources()
&& (filter.isEmpty() || (entity.getName().getString().toLowerCase(Locale.US).contains(filter.toLowerCase(Locale.US))
|| (entity.toString().toLowerCase(Locale.US).contains(filter.toLowerCase(Locale.US))))))
{
entities.mergeInt(entity.getType(), 1, Integer::sum);
entities.computeIfAbsent(entity.getType(), k -> new ArrayList<>()).add(entity);
}
}

Expand All @@ -479,7 +487,7 @@ private void updateResources()
final List<ItemStack> itemList = handler.getRequiredItems(world, here, blockState, tileEntity == null ? null : tileEntity.saveWithFullMetadata(), true);
for (final ItemStack stack : itemList)
{
addNeededResource(stack, 1);
addNeededResource(stack, stack.getCount());
}
handled = true;
break;
Expand Down Expand Up @@ -535,7 +543,7 @@ public void updateEntitylist()
{
entityList.enable();
entityList.show();
final List<EntityType> tempEntities = new ArrayList<>(entities.keySet());
final List<List<Entity>> tempEntities = entities.values().stream().toList();

//Creates a dataProvider for the unemployed resourceList.
entityList.setDataProvider(new ScrollingList.DataProvider()
Expand All @@ -559,23 +567,38 @@ public int getElementCount()
@Override
public void updateElement(final int index, final Pane rowPane)
{
final EntityType entity = tempEntities.get(index);
ItemStack entityIcon = entity.create(Minecraft.getInstance().level).getPickResult();
if (entity == EntityType.GLOW_ITEM_FRAME)
{
entityIcon = new ItemStack(Items.GLOW_ITEM_FRAME);
}
else if (entity == EntityType.ITEM_FRAME)
final List<Entity> entities = tempEntities.get(index);
boolean handled = false;
EntityType<?> entityType = entities.get(0).getType();
Entity tempEntity = entityType.create(entities.get(0).level());
ItemStack entityIcon = tempEntity.getPickResult();
List<ItemStorage> requiredItems = List.of();
for (final IEntityHandler entityHandler : EntityHandlers.handlers)
{
entityIcon = new ItemStack(Items.ITEM_FRAME);
if (entityHandler.canHandle(entities.get(0)))
{
handled = entityHandler.canPlace(entities.get(0), false, true);
entityIcon = entityHandler.getIcon(tempEntity);

// unique items in descending order of required count
requiredItems = entities.stream()
.flatMap(e -> entityHandler.getRequiredItems(e).stream())
.filter(s -> !s.isEmpty()).map(ItemStorage::new)
.collect(Collectors.toMap(s -> s, s -> s, (s1, s2) -> new ItemStorage(s1.getItemStack(), s1.getAmount() + s2.getAmount(), false)))
.values().stream().sorted(Comparator.comparingInt(ItemStorage::getAmount).reversed())
.toList();
break;
}
}
else if (entity == EntityType.MINECART)
rowPane.findPaneOfTypeByID(RESOURCE_QUANTITY_MISSING, Text.class).setText(Component.literal(Integer.toString(entities.size())));
rowPane.findPaneOfTypeByID(RESOURCE_ICON, ItemIcon.class).setItem(entityIcon);
rowPane.findPaneOfTypeByID(RESOURCE_NAME, Text.class).setText(entityType.getDescription());
for (int i = 0; i < 4; ++i)
{
entityIcon = new ItemStack(Items.MINECART);
rowPane.findPaneOfTypeByID(RESOURCE_ICON + (i + 1), ItemIcon.class)
.setItem(i < requiredItems.size() ? requiredItems.get(i).getItemStack() : ItemStack.EMPTY);
}
rowPane.findPaneOfTypeByID(RESOURCE_QUANTITY_MISSING, Text.class).setText(Component.literal(Integer.toString(entities.getInt(entity))));
rowPane.findPaneOfTypeByID(RESOURCE_ICON, ItemIcon.class).setItem(entityIcon);
rowPane.findPaneOfTypeByID(RESOURCE_NAME, Text.class).setText(entity.getDescription());
rowPane.findPaneOfTypeByID(RESOURCE_STATUS, Image.class).setImage(handled ? STATUS_HANDLED : STATUS_UNHANDLED, true);
if (!Minecraft.getInstance().player.isCreative())
{
rowPane.findPaneOfTypeByID(BUTTON_REMOVE_ENTITY, Button.class).hide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ private BlockPos getTeleportPos(@NotNull final BoxPreviewData box)
final Direction direction = Structurize.getConfig().getServer().teleportBuildDirection.get();
final int offset = Structurize.getConfig().getServer().teleportBuildDistance.get();

final AABB bounds = new AABB(box.getPos1(), box.getPos2());
final AABB bounds = box.getAABB();
final int size = (int) Math.round(bounds.max(direction.getAxis()) - bounds.min(direction.getAxis()));

return BlockPos.containing(bounds.getCenter()).atY((int) bounds.minY).relative(direction, offset + size / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.phys.AABB;

import java.util.List;
Expand Down Expand Up @@ -52,7 +53,7 @@ public RemoveEntityOperation(final Player player, final BlockPos startPos, final
@Override
public boolean apply(final ServerLevel world)
{
final List<Entity> list = world.getEntitiesOfClass(Entity.class, new AABB(startPos, endPos));
final List<Entity> list = world.getEntitiesOfClass(Entity.class, AABB.of(BoundingBox.fromCorners(startPos, endPos)));
storage.addEntities(list);

int count = 0;
Expand Down
Loading