Skip to content

Commit

Permalink
feat: ✨ Changed upgrade code to allow carts running them
Browse files Browse the repository at this point in the history
  • Loading branch information
P3pp3rF1y committed Dec 31, 2024
1 parent bf05bcd commit cc0c91f
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 85 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedcore
mod_group_id=sophisticatedcore
mod_version=1.0.4
mod_version=1.0.5
sonar_project_key=sophisticatedcore:SophisticatedCore
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedCore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public interface IStorageWrapper {

void setSaveHandler(Runnable saveHandler);
void setContentsChangeHandler(Runnable saveHandler);

default void setInventorySlotChangeHandler(Runnable slotChangeHandler) {
//noop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
Expand Down Expand Up @@ -101,6 +102,8 @@ protected StorageContainerMenuBase(MenuType<?> pMenuType, int pContainerId, Play

public abstract Optional<BlockPos> getBlockPosition();

public abstract Optional<Entity> getEntity();

protected void initSlotsAndContainers(Player player, int storageItemSlotIndex, boolean shouldLockStorageItemSlot) {
addStorageInventorySlots();
addPlayerInventorySlots(player.getInventory(), storageItemSlotIndex, shouldLockStorageItemSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class PacketHandler {
private final SimpleChannel networkWrapper;
private int idx = 0;

public PacketHandler(String modId) {
protected PacketHandler(String modId) {
networkWrapper = NetworkRegistry.newSimpleChannel(new ResourceLocation(modId, "channel"),
() -> PROTOCOL, PROTOCOL::equals, PROTOCOL::equals);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.p3pp3rf1y.sophisticatedcore.renderdata;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.*;
import net.minecraft.world.item.ItemStack;
import net.p3pp3rf1y.sophisticatedcore.upgrades.IRenderedBatteryUpgrade;
import net.p3pp3rf1y.sophisticatedcore.upgrades.IRenderedTankUpgrade;
Expand All @@ -12,15 +9,10 @@
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public abstract class RenderInfo {
private static final String TANKS_TAG = "tanks";
Expand All @@ -42,6 +34,7 @@ public abstract class RenderInfo {

private ItemDisplayRenderInfo itemDisplayRenderInfo;
private final Supplier<Runnable> getSaveHandler;
private final boolean showsCountsAndFillRatios;
private final List<ItemStack> upgradeItems = new ArrayList<>();
private final Map<UpgradeRenderDataType<?>, IUpgradeRenderData> upgradeData = new HashMap<>();

Expand All @@ -52,7 +45,12 @@ public abstract class RenderInfo {
private Consumer<RenderInfo> changeListener = ri -> {};

protected RenderInfo(Supplier<Runnable> getSaveHandler) {
this(getSaveHandler, false);
}

protected RenderInfo(Supplier<Runnable> getSaveHandler, boolean showsCountsAndFillRatios) {
this.getSaveHandler = getSaveHandler;
this.showsCountsAndFillRatios = showsCountsAndFillRatios;
itemDisplayRenderInfo = new ItemDisplayRenderInfo();
}

Expand Down Expand Up @@ -98,8 +96,8 @@ private void serializeUpgradeData(Consumer<CompoundTag> modifyUpgradesTag) {
serializeRenderInfo(renderInfo);
}

public void refreshItemDisplayRenderInfo(List<DisplayItem> displayItems, List<Integer> inaccessibleSlots) {
itemDisplayRenderInfo = new ItemDisplayRenderInfo(displayItems, inaccessibleSlots);
public void refreshItemDisplayRenderInfo(List<DisplayItem> displayItems, List<Integer> inaccessibleSlots, List<Integer> slotCounts, List<Float> slotFillRatios) {
itemDisplayRenderInfo = new ItemDisplayRenderInfo(displayItems, inaccessibleSlots, slotCounts, slotFillRatios);
CompoundTag renderInfo = getRenderInfoTag().orElse(new CompoundTag());
renderInfo.put(ITEM_DISPLAY_TAG, itemDisplayRenderInfo.serialize());
serializeRenderInfo(renderInfo);
Expand Down Expand Up @@ -254,23 +252,33 @@ public List<ItemStack> getUpgradeItems() {
return upgradeItems;
}

public boolean showsCountsAndFillRatios() {
return showsCountsAndFillRatios;
}

public static class ItemDisplayRenderInfo {
private static final String ITEMS_TAG = "items";
private static final String INACCESSIBLE_SLOTS_TAG = "inaccessibleSlots";
public static final String SLOT_COUNTS_TAG = "slotCounts";
public static final String SLOT_FILL_RATIOS_TAG = "slotFillRatios";
private final List<DisplayItem> displayItems;
private final List<Integer> inaccessibleSlots;
private final List<Integer> slotCounts;
private final List<Float> slotFillRatios;

private ItemDisplayRenderInfo(DisplayItem displayItem, List<Integer> inaccessibleSlots) {
this(List.of(displayItem), inaccessibleSlots);
private ItemDisplayRenderInfo(DisplayItem displayItem, List<Integer> inaccessibleSlots, List<Integer> slotCounts, List<Float> slotFillRatios) {
this(List.of(displayItem), inaccessibleSlots, slotCounts, slotFillRatios);
}

private ItemDisplayRenderInfo(List<DisplayItem> displayItems, List<Integer> inaccessibleSlots) {
private ItemDisplayRenderInfo(List<DisplayItem> displayItems, List<Integer> inaccessibleSlots, List<Integer> slotCounts, List<Float> slotFillRatios) {
this.displayItems = displayItems;
this.inaccessibleSlots = inaccessibleSlots;
this.slotCounts = slotCounts;
this.slotFillRatios = slotFillRatios;
}

public ItemDisplayRenderInfo() {
this(new ArrayList<>(), new ArrayList<>());
this(new ArrayList<>(), new ArrayList<>(), Collections.emptyList(), Collections.emptyList());
}

public CompoundTag serialize() {
Expand All @@ -281,16 +289,20 @@ public CompoundTag serialize() {
NBTHelper.putList(ret, ITEMS_TAG, displayItems, displayItem -> displayItem.serialize(new CompoundTag()));
}
NBTHelper.putList(ret, INACCESSIBLE_SLOTS_TAG, inaccessibleSlots, IntTag::valueOf);
ret.putIntArray(SLOT_COUNTS_TAG, slotCounts.stream().mapToInt(i -> i).toArray());
NBTHelper.putList(ret, SLOT_FILL_RATIOS_TAG, slotFillRatios, FloatTag::valueOf);
return ret;
}

public static ItemDisplayRenderInfo deserialize(CompoundTag tag) {
List<Integer> inaccessibleSlots = NBTHelper.getCollection(tag, INACCESSIBLE_SLOTS_TAG, Tag.TAG_INT, t -> Optional.of(((IntTag) t).getAsInt()), ArrayList::new).orElseGet(ArrayList::new);
List<Integer> slotCounts = Arrays.stream(tag.getIntArray(SLOT_COUNTS_TAG)).boxed().collect(Collectors.toCollection(ArrayList::new));
List<Float> slotFillRatios = NBTHelper.getCollection(tag, SLOT_FILL_RATIOS_TAG, Tag.TAG_FLOAT, t -> Optional.of(((FloatTag) t).getAsFloat()), ArrayList::new).orElseGet(ArrayList::new);
if (tag.contains(DisplayItem.ITEM_TAG)) {
return new ItemDisplayRenderInfo(DisplayItem.deserialize(tag), inaccessibleSlots);
return new ItemDisplayRenderInfo(DisplayItem.deserialize(tag), inaccessibleSlots, slotCounts, slotFillRatios);
} else if (tag.contains(ITEMS_TAG)) {
List<DisplayItem> items = NBTHelper.getCollection(tag, ITEMS_TAG, Tag.TAG_COMPOUND, stackTag -> Optional.of(DisplayItem.deserialize((CompoundTag) stackTag)), ArrayList::new).orElseGet(ArrayList::new);
return new ItemDisplayRenderInfo(items, inaccessibleSlots);
return new ItemDisplayRenderInfo(items, inaccessibleSlots, slotCounts, slotFillRatios);
}
return new ItemDisplayRenderInfo();
}
Expand All @@ -310,6 +322,14 @@ public List<DisplayItem> getDisplayItems() {
public List<Integer> getInaccessibleSlots() {
return inaccessibleSlots;
}

public List<Integer> getSlotCounts() {
return slotCounts;
}

public List<Float> getSlotFillRatios() {
return slotFillRatios;
}
}

public static class DisplayItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.p3pp3rf1y.sophisticatedcore.settings.ISlotColorCategory;
import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory;
import net.p3pp3rf1y.sophisticatedcore.util.ColorHelper;
import net.p3pp3rf1y.sophisticatedcore.util.MathHelper;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;

import java.util.ArrayList;
Expand Down Expand Up @@ -84,32 +85,67 @@ private boolean haveRenderedItemsChanged() {
}

int i = 0;
InventoryHandler inventoryHandler = inventoryHandlerSupplier.get();
for (int slotIndex : slotIndexes) {
ItemStack newItem = getSlotItemCopy(slotIndex).orElse(ItemStack.EMPTY);

if (ItemStackKey.getHashCode(newItem) != ItemStackKey.getHashCode(previousDisplayItems.get(i).getItem())
|| (inaccessibleSlots.contains(slotIndex) == inventoryHandlerSupplier.get().isSlotAccessible(slotIndex))) {
|| (inaccessibleSlots.contains(slotIndex) == inventoryHandler.isSlotAccessible(slotIndex))) {
return true;
}

i++;
}

if (renderInfoSupplier.get().showsCountsAndFillRatios()) {
List<Integer> previousSlotCounts = renderInfoSupplier.get().getItemDisplayRenderInfo().getSlotCounts();
List<Float> previousSlotFillRatios = renderInfoSupplier.get().getItemDisplayRenderInfo().getSlotFillRatios();

if (previousSlotCounts.size() != inventoryHandler.getSlots() || previousSlotFillRatios.size() != inventoryHandler.getSlots()) {
return true;
}

for (int slotIndex = 0; slotIndex < inventoryHandler.getSlots(); slotIndex++) {
int previousSlotCount = previousSlotCounts.get(slotIndex);
float previousSlotFillRatio = previousSlotFillRatios.get(slotIndex);
ItemStack stack = inventoryHandler.getStackInSlot(slotIndex);
float currentSlotFillRatio = calculateSlotFillRatio(stack, inventoryHandler, slotIndex);
if (previousSlotCount != stack.getCount() || !MathHelper.epsilonEquals(previousSlotFillRatio, currentSlotFillRatio)) {
return true;
}
}
}

return i != previousDisplayItems.size();
}

private void updateFullRenderInfo() {
List<RenderInfo.DisplayItem> displayItems = new ArrayList<>();
List<Integer> inaccessibleSlots = new ArrayList<>();
InventoryHandler inventoryHandler = inventoryHandlerSupplier.get();
for (int slotIndex : slotIndexes) {
getSlotItemCopy(slotIndex).ifPresent(stackCopy ->
displayItems.add(new RenderInfo.DisplayItem(stackCopy, slotRotations.getOrDefault(slotIndex, 0), slotIndex, displaySide)));
if (!inventoryHandlerSupplier.get().isSlotAccessible(slotIndex)) {
displayItems.add(new RenderInfo.DisplayItem(getSlotItemCopy(slotIndex).orElse(ItemStack.EMPTY), slotRotations.getOrDefault(slotIndex, 0), slotIndex, displaySide));
if (!inventoryHandler.isSlotAccessible(slotIndex)) {
inaccessibleSlots.add(slotIndex);
}
}

renderInfoSupplier.get().refreshItemDisplayRenderInfo(displayItems, inaccessibleSlots);
List<Integer> slotCounts = new ArrayList<>();
List<Float> slotFillRatios = new ArrayList<>();

if (renderInfoSupplier.get().showsCountsAndFillRatios()) {
for (int slotIndex = 0; slotIndex < inventoryHandler.getSlots(); slotIndex++) {
ItemStack stack = inventoryHandler.getStackInSlot(slotIndex);
slotCounts.add(stack.getCount());
slotFillRatios.add(calculateSlotFillRatio(stack, inventoryHandler, slotIndex));
}
}

renderInfoSupplier.get().refreshItemDisplayRenderInfo(displayItems, inaccessibleSlots, slotCounts, slotFillRatios);
}

private static float calculateSlotFillRatio(ItemStack stack, InventoryHandler inventoryHandler, int slotIndex) {
return stack.isEmpty() ? 0 : (float) stack.getCount() / inventoryHandler.getStackLimit(slotIndex, stack);
}

private Optional<ItemStack> getSlotItemCopy(int slotIndex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package net.p3pp3rf1y.sophisticatedcore.upgrades;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;

import javax.annotation.Nullable;

public interface ITickableUpgrade {
void tick(@Nullable LivingEntity entity, Level world, BlockPos pos);
void tick(@Nullable Entity entity, Level world, BlockPos pos);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.p3pp3rf1y.sophisticatedcore.upgrades.battery;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
Expand Down Expand Up @@ -146,7 +146,7 @@ public void forceUpdateBatteryRenderInfo() {
}

@Override
public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) {
public void tick(@Nullable Entity entity, Level world, BlockPos pos) {
if (energyStored < getMaxEnergyStored()) {
inventory.getStackInSlot(INPUT_SLOT).getCapability(ForgeCapabilities.ENERGY).ifPresent(this::receiveFromStorage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package net.p3pp3rf1y.sophisticatedcore.upgrades.compacting;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.items.IItemHandler;
import net.p3pp3rf1y.sophisticatedcore.api.ISlotChangeResponseUpgrade;
import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper;
import net.p3pp3rf1y.sophisticatedcore.inventory.IItemHandlerSimpleInserter;
import net.p3pp3rf1y.sophisticatedcore.upgrades.FilterLogic;
import net.p3pp3rf1y.sophisticatedcore.upgrades.IFilteredUpgrade;
import net.p3pp3rf1y.sophisticatedcore.upgrades.IInsertResponseUpgrade;
import net.p3pp3rf1y.sophisticatedcore.upgrades.ITickableUpgrade;
import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeWrapperBase;
import net.p3pp3rf1y.sophisticatedcore.upgrades.*;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
import net.p3pp3rf1y.sophisticatedcore.util.RecipeHelper;
Expand Down Expand Up @@ -130,7 +126,7 @@ public boolean shouldWorkInGUI() {
}

@Override
public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) {
public void tick(@Nullable Entity entity, Level world, BlockPos pos) {
if (slotsToCompact.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package net.p3pp3rf1y.sophisticatedcore.upgrades.cooking;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.item.crafting.BlastingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.item.crafting.SmokingRecipe;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.items.IItemHandlerModifiable;
Expand Down Expand Up @@ -101,7 +97,7 @@ private void tryPushingOutput() {
}

@Override
public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) {
public void tick(@Nullable Entity entity, Level world, BlockPos pos) {
if (isInCooldown(world)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package net.p3pp3rf1y.sophisticatedcore.upgrades.cooking;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.item.crafting.BlastingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.item.crafting.SmokingRecipe;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper;
import net.p3pp3rf1y.sophisticatedcore.renderdata.RenderInfo;
Expand All @@ -28,7 +24,8 @@ protected CookingUpgradeWrapper(IStorageWrapper storageWrapper, ItemStack upgrad
cookingLogic = new CookingLogic<>(upgrade, upgradeSaveHandler, upgradeItem.getCookingUpgradeConfig(), recipeType, burnTimeModifier);
}

public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) {
@Override
public void tick(@Nullable Entity entity, Level world, BlockPos pos) {
if (isInCooldown(world)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -38,7 +39,7 @@ public FeedingUpgradeWrapper(IStorageWrapper storageWrapper, ItemStack upgrade,
}

@Override
public void tick(@Nullable LivingEntity entity, Level level, BlockPos pos) {
public void tick(@Nullable Entity entity, Level level, BlockPos pos) {
if (isInCooldown(level) || (entity != null && !(entity instanceof Player))) {
return;
}
Expand Down
Loading

0 comments on commit cc0c91f

Please sign in to comment.