From cc0c91f976ff6f5aac4ccd0df1fc0ece8f3d2fc7 Mon Sep 17 00:00:00 2001 From: P3pp3rF1y Date: Tue, 31 Dec 2024 20:57:45 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Changed=20upgrade=20code=20?= =?UTF-8?q?to=20allow=20carts=20running=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../api/IStorageWrapper.java | 2 +- .../common/gui/StorageContainerMenuBase.java | 3 + .../network/PacketHandler.java | 2 +- .../renderdata/RenderInfo.java | 58 +++++++++++++------ .../ItemDisplaySettingsCategory.java | 46 +++++++++++++-- .../upgrades/ITickableUpgrade.java | 3 +- .../battery/BatteryUpgradeWrapper.java | 4 +- .../compacting/CompactingUpgradeWrapper.java | 10 +--- .../cooking/AutoCookingUpgradeWrapper.java | 10 +--- .../cooking/CookingUpgradeWrapper.java | 11 ++-- .../feeding/FeedingUpgradeWrapper.java | 3 +- .../upgrades/jukebox/JukeboxUpgradeItem.java | 3 +- .../upgrades/magnet/MagnetUpgradeWrapper.java | 23 +++----- .../upgrades/pump/PumpUpgradeWrapper.java | 6 +- .../upgrades/tank/TankUpgradeWrapper.java | 4 +- .../upgrades/voiding/VoidUpgradeWrapper.java | 13 +---- .../upgrades/xppump/XpPumpUpgradeWrapper.java | 4 +- .../sophisticatedcore/util/MathHelper.java | 6 ++ .../util/NoopStorageWrapper.java | 2 +- 20 files changed, 130 insertions(+), 85 deletions(-) diff --git a/gradle.properties b/gradle.properties index 20079cde..712a3553 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/api/IStorageWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/api/IStorageWrapper.java index 511f4421..b79dbb66 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/api/IStorageWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/api/IStorageWrapper.java @@ -16,7 +16,7 @@ public interface IStorageWrapper { - void setSaveHandler(Runnable saveHandler); + void setContentsChangeHandler(Runnable saveHandler); default void setInventorySlotChangeHandler(Runnable slotChangeHandler) { //noop diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/common/gui/StorageContainerMenuBase.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/common/gui/StorageContainerMenuBase.java index a1a37d6e..669b6154 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/common/gui/StorageContainerMenuBase.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/common/gui/StorageContainerMenuBase.java @@ -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.*; @@ -101,6 +102,8 @@ protected StorageContainerMenuBase(MenuType pMenuType, int pContainerId, Play public abstract Optional getBlockPosition(); + public abstract Optional getEntity(); + protected void initSlotsAndContainers(Player player, int storageItemSlotIndex, boolean shouldLockStorageItemSlot) { addStorageInventorySlots(); addPlayerInventorySlots(player.getInventory(), storageItemSlotIndex, shouldLockStorageItemSlot); diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/network/PacketHandler.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/network/PacketHandler.java index a31d46b7..c01445bc 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/network/PacketHandler.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/network/PacketHandler.java @@ -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); } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/renderdata/RenderInfo.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/renderdata/RenderInfo.java index 3f35abfb..ebbfb053 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/renderdata/RenderInfo.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/renderdata/RenderInfo.java @@ -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; @@ -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"; @@ -42,6 +34,7 @@ public abstract class RenderInfo { private ItemDisplayRenderInfo itemDisplayRenderInfo; private final Supplier getSaveHandler; + private final boolean showsCountsAndFillRatios; private final List upgradeItems = new ArrayList<>(); private final Map, IUpgradeRenderData> upgradeData = new HashMap<>(); @@ -52,7 +45,12 @@ public abstract class RenderInfo { private Consumer changeListener = ri -> {}; protected RenderInfo(Supplier getSaveHandler) { + this(getSaveHandler, false); + } + + protected RenderInfo(Supplier getSaveHandler, boolean showsCountsAndFillRatios) { this.getSaveHandler = getSaveHandler; + this.showsCountsAndFillRatios = showsCountsAndFillRatios; itemDisplayRenderInfo = new ItemDisplayRenderInfo(); } @@ -98,8 +96,8 @@ private void serializeUpgradeData(Consumer modifyUpgradesTag) { serializeRenderInfo(renderInfo); } - public void refreshItemDisplayRenderInfo(List displayItems, List inaccessibleSlots) { - itemDisplayRenderInfo = new ItemDisplayRenderInfo(displayItems, inaccessibleSlots); + public void refreshItemDisplayRenderInfo(List displayItems, List inaccessibleSlots, List slotCounts, List slotFillRatios) { + itemDisplayRenderInfo = new ItemDisplayRenderInfo(displayItems, inaccessibleSlots, slotCounts, slotFillRatios); CompoundTag renderInfo = getRenderInfoTag().orElse(new CompoundTag()); renderInfo.put(ITEM_DISPLAY_TAG, itemDisplayRenderInfo.serialize()); serializeRenderInfo(renderInfo); @@ -254,23 +252,33 @@ public List 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 displayItems; private final List inaccessibleSlots; + private final List slotCounts; + private final List slotFillRatios; - private ItemDisplayRenderInfo(DisplayItem displayItem, List inaccessibleSlots) { - this(List.of(displayItem), inaccessibleSlots); + private ItemDisplayRenderInfo(DisplayItem displayItem, List inaccessibleSlots, List slotCounts, List slotFillRatios) { + this(List.of(displayItem), inaccessibleSlots, slotCounts, slotFillRatios); } - private ItemDisplayRenderInfo(List displayItems, List inaccessibleSlots) { + private ItemDisplayRenderInfo(List displayItems, List inaccessibleSlots, List slotCounts, List 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() { @@ -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 inaccessibleSlots = NBTHelper.getCollection(tag, INACCESSIBLE_SLOTS_TAG, Tag.TAG_INT, t -> Optional.of(((IntTag) t).getAsInt()), ArrayList::new).orElseGet(ArrayList::new); + List slotCounts = Arrays.stream(tag.getIntArray(SLOT_COUNTS_TAG)).boxed().collect(Collectors.toCollection(ArrayList::new)); + List 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 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(); } @@ -310,6 +322,14 @@ public List getDisplayItems() { public List getInaccessibleSlots() { return inaccessibleSlots; } + + public List getSlotCounts() { + return slotCounts; + } + + public List getSlotFillRatios() { + return slotFillRatios; + } } public static class DisplayItem { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/settings/itemdisplay/ItemDisplaySettingsCategory.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/settings/itemdisplay/ItemDisplaySettingsCategory.java index 6bc55fa1..d534f940 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/settings/itemdisplay/ItemDisplaySettingsCategory.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/settings/itemdisplay/ItemDisplaySettingsCategory.java @@ -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; @@ -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 previousSlotCounts = renderInfoSupplier.get().getItemDisplayRenderInfo().getSlotCounts(); + List 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 displayItems = new ArrayList<>(); List 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 slotCounts = new ArrayList<>(); + List 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 getSlotItemCopy(int slotIndex) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/ITickableUpgrade.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/ITickableUpgrade.java index fd963274..62e3378a 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/ITickableUpgrade.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/ITickableUpgrade.java @@ -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); } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/battery/BatteryUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/battery/BatteryUpgradeWrapper.java index 93dfad2d..80b86411 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/battery/BatteryUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/battery/BatteryUpgradeWrapper.java @@ -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; @@ -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); } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/compacting/CompactingUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/compacting/CompactingUpgradeWrapper.java index 4bd8bcef..57cf0fe1 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/compacting/CompactingUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/compacting/CompactingUpgradeWrapper.java @@ -1,7 +1,7 @@ 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; @@ -9,11 +9,7 @@ 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; @@ -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; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/AutoCookingUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/AutoCookingUpgradeWrapper.java index c8a45d8d..fd83af31 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/AutoCookingUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/AutoCookingUpgradeWrapper.java @@ -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; @@ -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; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/CookingUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/CookingUpgradeWrapper.java index 12abde77..9b1818b2 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/CookingUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/cooking/CookingUpgradeWrapper.java @@ -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; @@ -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; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/feeding/FeedingUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/feeding/FeedingUpgradeWrapper.java index 76c3901b..3a9ff509 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/feeding/FeedingUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/feeding/FeedingUpgradeWrapper.java @@ -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; @@ -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; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/jukebox/JukeboxUpgradeItem.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/jukebox/JukeboxUpgradeItem.java index d65acf30..5b9809b8 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/jukebox/JukeboxUpgradeItem.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/jukebox/JukeboxUpgradeItem.java @@ -2,6 +2,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -119,7 +120,7 @@ public IItemHandler getDiscInventory() { } @Override - public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) { + public void tick(@Nullable Entity entity, Level world, BlockPos pos) { if (isPlaying && lastKeepAliveSendTime < world.getGameTime() - KEEP_ALIVE_SEND_INTERVAL) { storageWrapper.getContentsUuid().ifPresent(storageUuid -> ServerStorageSoundHandler.updateKeepAlive(storageUuid, world, entity != null ? entity.position() : Vec3.atCenterOf(pos), () -> setIsPlaying(false)) diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/magnet/MagnetUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/magnet/MagnetUpgradeWrapper.java index 4e7cb12d..0e58950b 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/magnet/MagnetUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/magnet/MagnetUpgradeWrapper.java @@ -7,7 +7,6 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.ExperienceOrb; -import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -19,11 +18,7 @@ import net.p3pp3rf1y.sophisticatedcore.init.ModFluids; import net.p3pp3rf1y.sophisticatedcore.inventory.IItemHandlerSimpleInserter; import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory; -import net.p3pp3rf1y.sophisticatedcore.upgrades.ContentsFilterLogic; -import net.p3pp3rf1y.sophisticatedcore.upgrades.IContentsFilteredUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.IPickupResponseUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.ITickableUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeWrapperBase; +import net.p3pp3rf1y.sophisticatedcore.upgrades.*; import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper; import net.p3pp3rf1y.sophisticatedcore.util.XpHelper; @@ -69,7 +64,7 @@ public ItemStack pickup(Level world, ItemStack stack, boolean simulate) { } @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; } @@ -87,7 +82,7 @@ private boolean canFillStorageWithXp() { return storageWrapper.getFluidHandler().map(fluidHandler -> fluidHandler.fill(ModFluids.EXPERIENCE_TAG, 1, ModFluids.XP_STILL.get(), IFluidHandler.FluidAction.SIMULATE) > 0).orElse(false); } - private int pickupXpOrbs(@Nullable LivingEntity entity, Level world, BlockPos pos) { + private int pickupXpOrbs(@Nullable Entity entity, Level world, BlockPos pos) { List xpEntities = world.getEntitiesOfClass(ExperienceOrb.class, new AABB(pos).inflate(upgradeItem.getRadius()), e -> true); if (xpEntities.isEmpty()) { return COOLDOWN_TICKS; @@ -103,7 +98,7 @@ private int pickupXpOrbs(@Nullable LivingEntity entity, Level world, BlockPos po return cooldown; } - private boolean tryToFillTank(ExperienceOrb xpOrb, @Nullable LivingEntity entity, Level world) { + private boolean tryToFillTank(ExperienceOrb xpOrb, @Nullable Entity entity, Level world) { int amountToTransfer = XpHelper.experienceToLiquid(xpOrb.getValue()); return storageWrapper.getFluidHandler().map(fluidHandler -> { @@ -129,7 +124,7 @@ private boolean tryToFillTank(ExperienceOrb xpOrb, @Nullable LivingEntity entity }).orElse(false); } - private int pickupItems(@Nullable LivingEntity entity, Level world, BlockPos pos) { + private int pickupItems(@Nullable Entity entity, Level world, BlockPos pos) { List itemEntities = world.getEntities(EntityType.ITEM, new AABB(pos).inflate(upgradeItem.getRadius()), e -> true); if (itemEntities.isEmpty()) { return COOLDOWN_TICKS; @@ -173,13 +168,13 @@ private boolean isBlockedBySomething(Entity entity) { return false; } - private boolean canNotPickup(Entity entity, @Nullable LivingEntity player) { - if (isBlockedBySomething(entity)) { + private boolean canNotPickup(Entity pickedUpEntity, @Nullable Entity entity) { + if (isBlockedBySomething(pickedUpEntity)) { return true; } - CompoundTag data = entity.getPersistentData(); - return player != null ? data.contains(PREVENT_REMOTE_MOVEMENT) : data.contains(PREVENT_REMOTE_MOVEMENT) && !data.contains(ALLOW_MACHINE_MOVEMENT); + CompoundTag data = pickedUpEntity.getPersistentData(); + return entity != null ? data.contains(PREVENT_REMOTE_MOVEMENT) : data.contains(PREVENT_REMOTE_MOVEMENT) && !data.contains(ALLOW_MACHINE_MOVEMENT); } private boolean tryToInsertItem(ItemEntity itemEntity) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/pump/PumpUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/pump/PumpUpgradeWrapper.java index efe0d6ee..2976d524 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/pump/PumpUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/pump/PumpUpgradeWrapper.java @@ -3,7 +3,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.InteractionHand; -import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; @@ -54,14 +54,14 @@ protected PumpUpgradeWrapper(IStorageWrapper storageWrapper, ItemStack upgrade, } @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; } setCooldown(world, storageWrapper.getFluidHandler().map(storageFluidHandler -> tick(storageFluidHandler, entity, world, pos)).orElse(DID_NOTHING_COOLDOWN_TIME)); } - private int tick(IFluidHandlerItem storageFluidHandler, @Nullable LivingEntity entity, Level level, BlockPos pos) { + private int tick(IFluidHandlerItem storageFluidHandler, @Nullable Entity entity, Level level, BlockPos pos) { if (entity == null) { Optional newCooldown = handleInWorldInteractions(storageFluidHandler, level, pos); if (newCooldown.isPresent()) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/tank/TankUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/tank/TankUpgradeWrapper.java index b205b1f8..c50d3c40 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/tank/TankUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/tank/TankUpgradeWrapper.java @@ -2,7 +2,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; -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; @@ -178,7 +178,7 @@ public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action, boolean } @Override - public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) { + public void tick(@Nullable Entity entity, Level world, BlockPos pos) { if (world.getGameTime() < cooldownTime) { return; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/voiding/VoidUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/voiding/VoidUpgradeWrapper.java index 7a288e8a..b2446bf6 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/voiding/VoidUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/voiding/VoidUpgradeWrapper.java @@ -1,7 +1,7 @@ package net.p3pp3rf1y.sophisticatedcore.upgrades.voiding; 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.items.IItemHandler; @@ -9,14 +9,7 @@ import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper; import net.p3pp3rf1y.sophisticatedcore.inventory.IItemHandlerSimpleInserter; import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler; -import net.p3pp3rf1y.sophisticatedcore.upgrades.FilterLogic; -import net.p3pp3rf1y.sophisticatedcore.upgrades.IFilteredUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.IInsertResponseUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.IOverflowResponseUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.ISlotLimitUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.ITickableUpgrade; -import net.p3pp3rf1y.sophisticatedcore.upgrades.PrimaryMatch; -import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeWrapperBase; +import net.p3pp3rf1y.sophisticatedcore.upgrades.*; import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper; import javax.annotation.Nullable; @@ -104,7 +97,7 @@ public void onSlotChange(IItemHandler inventoryHandler, int slot) { } @Override - public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) { + public void tick(@Nullable Entity entity, Level world, BlockPos pos) { if (slotsToVoid.isEmpty()) { return; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/xppump/XpPumpUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/xppump/XpPumpUpgradeWrapper.java index 55781672..c293ccac 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/xppump/XpPumpUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/upgrades/xppump/XpPumpUpgradeWrapper.java @@ -1,8 +1,8 @@ package net.p3pp3rf1y.sophisticatedcore.upgrades.xppump; import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EquipmentSlot; -import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.enchantment.EnchantmentHelper; @@ -37,7 +37,7 @@ protected XpPumpUpgradeWrapper(IStorageWrapper storageWrapper, ItemStack upgrade } @Override - public void tick(@Nullable LivingEntity entity, Level world, BlockPos pos) { + public void tick(@Nullable Entity entity, Level world, BlockPos pos) { if ((entity != null && !(entity instanceof Player)) || isInCooldown(world)) { return; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/MathHelper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/MathHelper.java index 0439c78a..ebbbda21 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/MathHelper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/MathHelper.java @@ -3,6 +3,8 @@ public class MathHelper { private MathHelper() {} + private static final float EPSILON = 1e-6f; + public static int intMaxCappedAddition(int a, int b) { return Integer.MAX_VALUE - a < b ? Integer.MAX_VALUE : a + b; } @@ -10,4 +12,8 @@ public static int intMaxCappedAddition(int a, int b) { public static int intMaxCappedMultiply(int a, int b) { return Integer.MAX_VALUE / a < b ? Integer.MAX_VALUE : a * b; } + + public static boolean epsilonEquals(float a, float b) { + return Math.abs(a - b) < EPSILON; + } } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/NoopStorageWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/NoopStorageWrapper.java index 0790dfff..c2407951 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/NoopStorageWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedcore/util/NoopStorageWrapper.java @@ -40,7 +40,7 @@ protected NoopStorageWrapper() { } @Override - public void setSaveHandler(Runnable saveHandler) { + public void setContentsChangeHandler(Runnable saveHandler) { //noop }