From 724abc6dbd8cf69f34433d672f5bc809db2bd6a2 Mon Sep 17 00:00:00 2001 From: Zontreck Date: Mon, 4 Mar 2024 16:33:36 -0700 Subject: [PATCH] Throttle Energy Sync packet to when energy is changed, on delayed tick, and only when screen is opened on the player. --- gradle.properties | 2 +- src/main/java/dev/zontreck/otemod/OTEMod.java | 11 + .../otemod/blocks/UncrafterBlock.java | 22 +- .../entity/CompressionChamberBlockEntity.java | 25 +- .../entity/ItemScrubberBlockEntity.java | 28 +- .../entity/MagicalScrubberBlockEntity.java | 25 +- .../blocks/entity/UncrafterBlockEntity.java | 280 +++++++++--------- .../BorrowedProtectionEnchantment.java | 33 --- .../otemod/enchantments/ModEnchantments.java | 2 - .../zontreck/otemod/events/ClientEvents.java | 36 +++ .../zontreck/otemod/events/EventHandler.java | 2 - .../compressor/CompressionChamberMenu.java | 13 +- .../compressor/CompressionChamberScreen.java | 16 +- .../energy/IThresholdsEnergy.java | 6 + .../energy/IThresholdsEnergyContainer.java | 13 + .../implementation/energy/OTEEnergy.java | 5 + .../scrubber/ItemScrubberMenu.java | 15 +- .../scrubber/ItemScrubberScreen.java | 16 +- .../scrubber/MagicalScrubberMenu.java | 15 +- .../scrubber/MagicalScrubberScreen.java | 15 +- .../uncrafting/UncrafterMenu.java | 16 +- .../uncrafting/UncrafterScreen.java | 15 +- .../otemod/networking/ModMessages.java | 7 + .../packets/EnergyRequestC2SPacket.java | 62 ++++ .../packets/EnergySyncS2CPacket.java | 12 +- 25 files changed, 466 insertions(+), 226 deletions(-) delete mode 100644 src/main/java/dev/zontreck/otemod/enchantments/BorrowedProtectionEnchantment.java create mode 100644 src/main/java/dev/zontreck/otemod/implementation/energy/IThresholdsEnergy.java create mode 100644 src/main/java/dev/zontreck/otemod/implementation/energy/IThresholdsEnergyContainer.java create mode 100644 src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java diff --git a/gradle.properties b/gradle.properties index 67f88f0..c8fb84c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ mod_name=Thresholds # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1201.4.030424.0229 +mod_version=1201.4.030424.1236 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/otemod/OTEMod.java b/src/main/java/dev/zontreck/otemod/OTEMod.java index e3f4713..2bc5e65 100644 --- a/src/main/java/dev/zontreck/otemod/OTEMod.java +++ b/src/main/java/dev/zontreck/otemod/OTEMod.java @@ -8,6 +8,7 @@ import dev.zontreck.libzontreck.profiles.Profile; import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException; import dev.zontreck.libzontreck.util.ChatHelpers; import dev.zontreck.libzontreck.vectors.Vector3; +import dev.zontreck.libzontreck.vectors.WorldPosition; import dev.zontreck.otemod.blocks.DeprecatedModBlocks; import dev.zontreck.otemod.configs.snbt.ServerConfig; import dev.zontreck.otemod.effects.ModEffects; @@ -16,21 +17,30 @@ import dev.zontreck.otemod.enchantments.NightVisionEnchantment; import dev.zontreck.otemod.events.EventHandler; import dev.zontreck.otemod.implementation.*; import dev.zontreck.otemod.implementation.compressor.CompressionChamberScreen; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer; import dev.zontreck.otemod.implementation.uncrafting.UncrafterScreen; import dev.zontreck.otemod.implementation.vault.*; import dev.zontreck.otemod.integrations.KeyBindings; import dev.zontreck.otemod.items.DeprecatedModItems; +import dev.zontreck.otemod.networking.packets.EnergyRequestC2SPacket; import dev.zontreck.otemod.recipe.ModRecipes; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.MenuScreens; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.RegisterKeyMappingsEvent; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -306,6 +316,7 @@ public class OTEMod ev.register(KeyBindings.OPEN_VAULT); } + } } diff --git a/src/main/java/dev/zontreck/otemod/blocks/UncrafterBlock.java b/src/main/java/dev/zontreck/otemod/blocks/UncrafterBlock.java index 0311e18..b1b0954 100644 --- a/src/main/java/dev/zontreck/otemod/blocks/UncrafterBlock.java +++ b/src/main/java/dev/zontreck/otemod/blocks/UncrafterBlock.java @@ -1,11 +1,14 @@ package dev.zontreck.otemod.blocks; +import dev.zontreck.otemod.blocks.entity.CompressionChamberBlockEntity; import dev.zontreck.otemod.blocks.entity.ModEntities; import dev.zontreck.otemod.blocks.entity.UncrafterBlockEntity; import dev.zontreck.otemod.networking.ModMessages; import dev.zontreck.otemod.networking.packets.EnergySyncS2CPacket; import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; @@ -26,8 +29,15 @@ import org.jetbrains.annotations.Nullable; public class UncrafterBlock extends HorizontalDirectionalBlock implements EntityBlock { - public UncrafterBlock(Properties p_49795_) { - super(p_49795_); + + public UncrafterBlock(Properties pProperties) { + super(pProperties); + } + + @Override + protected void createBlockStateDefinition(StateDefinition.Builder pBuilder) { + super.createBlockStateDefinition(pBuilder); + pBuilder.add(FACING); } @Nullable @@ -37,13 +47,7 @@ public class UncrafterBlock extends HorizontalDirectionalBlock implements Entity } @Override - protected void createBlockStateDefinition(StateDefinition.Builder p_49915_) { - super.createBlockStateDefinition(p_49915_); - p_49915_.add(FACING); - } - - @Override - public RenderShape getRenderShape(BlockState p_60550_) { + public RenderShape getRenderShape(BlockState pState) { return RenderShape.MODEL; } diff --git a/src/main/java/dev/zontreck/otemod/blocks/entity/CompressionChamberBlockEntity.java b/src/main/java/dev/zontreck/otemod/blocks/entity/CompressionChamberBlockEntity.java index 36e97e8..ac1156c 100644 --- a/src/main/java/dev/zontreck/otemod/blocks/entity/CompressionChamberBlockEntity.java +++ b/src/main/java/dev/zontreck/otemod/blocks/entity/CompressionChamberBlockEntity.java @@ -3,6 +3,7 @@ package dev.zontreck.otemod.blocks.entity; import dev.zontreck.libzontreck.util.ChatHelpers; import dev.zontreck.otemod.implementation.OutputItemStackHandler; import dev.zontreck.otemod.implementation.compressor.CompressionChamberMenu; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import dev.zontreck.otemod.implementation.energy.OTEEnergy; import dev.zontreck.otemod.networking.ModMessages; import dev.zontreck.otemod.networking.packets.EnergySyncS2CPacket; @@ -36,9 +37,12 @@ import net.minecraftforge.items.ItemStackHandler; import javax.annotation.Nullable; import java.util.Optional; -public class CompressionChamberBlockEntity extends BlockEntity implements MenuProvider +public class CompressionChamberBlockEntity extends BlockEntity implements MenuProvider, IThresholdsEnergy { + private boolean EnergyDirty=true; + private int TickCount=0; + public CompressionChamberBlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModEntities.COMPRESSION_CHAMBER.get(), pPos, pBlockState); @@ -93,8 +97,7 @@ public class CompressionChamberBlockEntity extends BlockEntity implements MenuPr @Override public void onChanged() { setChanged(); - - ModMessages.sendToAll(new EnergySyncS2CPacket(energy, getBlockPos())); + EnergyDirty=true; } }; @@ -201,6 +204,17 @@ public class CompressionChamberBlockEntity extends BlockEntity implements MenuPr if(lvl.isClientSide())return; + + if(entity.EnergyDirty) + { + if(entity.TickCount >= (2 * 20)) + { + ModMessages.sendToAll(new EnergySyncS2CPacket(entity.getEnergy(), pos)); + entity.EnergyDirty=false; + } else entity.TickCount++; + } + + if(hasRecipe(entity)) { if(!hasEnergy(entity))return; // Halt until sufficient energy has been received @@ -305,9 +319,14 @@ public class CompressionChamberBlockEntity extends BlockEntity implements MenuPr return ENERGY_STORAGE; } + @Override public void setEnergy(int energy) { ENERGY_STORAGE.setEnergy(energy); } + @Override + public int getEnergy() { + return ENERGY_STORAGE.getEnergy(); + } } diff --git a/src/main/java/dev/zontreck/otemod/blocks/entity/ItemScrubberBlockEntity.java b/src/main/java/dev/zontreck/otemod/blocks/entity/ItemScrubberBlockEntity.java index 19ba81d..4c1c1a1 100644 --- a/src/main/java/dev/zontreck/otemod/blocks/entity/ItemScrubberBlockEntity.java +++ b/src/main/java/dev/zontreck/otemod/blocks/entity/ItemScrubberBlockEntity.java @@ -1,6 +1,7 @@ package dev.zontreck.otemod.blocks.entity; import dev.zontreck.otemod.implementation.OutputItemStackHandler; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import dev.zontreck.otemod.implementation.energy.OTEEnergy; import dev.zontreck.otemod.implementation.scrubber.ItemScrubberMenu; import dev.zontreck.otemod.networking.ModMessages; @@ -30,9 +31,18 @@ import net.minecraftforge.items.ItemStackHandler; import javax.annotation.Nullable; -public class ItemScrubberBlockEntity extends BlockEntity implements MenuProvider +public class ItemScrubberBlockEntity extends BlockEntity implements MenuProvider, IThresholdsEnergy { + private boolean EnergyDirty=true; + private int TickCount=0; + + + @Override + public int getEnergy() { + return ENERGY_STORAGE.getEnergy(); + } + protected final ItemStackHandler itemsHandler = new ItemStackHandler(1){ @Override protected void onContentsChanged(int slot) @@ -54,13 +64,12 @@ public class ItemScrubberBlockEntity extends BlockEntity implements MenuProvider @Override public void onChanged() { setChanged(); - - ModMessages.sendToAll(new EnergySyncS2CPacket(energy, getBlockPos())); + EnergyDirty=true; } }; - private static final int ENERGY_REQ = 1500; + private static final int ENERGY_REQ = 512; private LazyOptional lazyEnergyHandler = LazyOptional.empty(); private LazyOptional lazyItemHandler = LazyOptional.empty(); @@ -194,6 +203,16 @@ public class ItemScrubberBlockEntity extends BlockEntity implements MenuProvider if(lvl.isClientSide())return; + + if(entity.EnergyDirty) + { + if(entity.TickCount >= (2 * 20)) + { + ModMessages.sendToAll(new EnergySyncS2CPacket(entity.getEnergy(), pos)); + entity.EnergyDirty=false; + } else entity.TickCount++; + } + if(hasRecipe(entity)) { if(!hasEnergy(entity))return; // Halt until sufficient energy has been received @@ -286,6 +305,7 @@ public class ItemScrubberBlockEntity extends BlockEntity implements MenuProvider return ENERGY_STORAGE; } + @Override public void setEnergy(int energy) { ENERGY_STORAGE.setEnergy(energy); } diff --git a/src/main/java/dev/zontreck/otemod/blocks/entity/MagicalScrubberBlockEntity.java b/src/main/java/dev/zontreck/otemod/blocks/entity/MagicalScrubberBlockEntity.java index 4e56a86..e44909b 100644 --- a/src/main/java/dev/zontreck/otemod/blocks/entity/MagicalScrubberBlockEntity.java +++ b/src/main/java/dev/zontreck/otemod/blocks/entity/MagicalScrubberBlockEntity.java @@ -2,6 +2,7 @@ package dev.zontreck.otemod.blocks.entity; import dev.zontreck.libzontreck.util.ItemUtils; import dev.zontreck.otemod.implementation.OutputItemStackHandler; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import dev.zontreck.otemod.implementation.energy.OTEEnergy; import dev.zontreck.otemod.implementation.scrubber.MagicalScrubberMenu; import dev.zontreck.otemod.networking.ModMessages; @@ -36,9 +37,17 @@ import javax.annotation.Nullable; import java.util.Iterator; import java.util.Map; -public class MagicalScrubberBlockEntity extends BlockEntity implements MenuProvider +public class MagicalScrubberBlockEntity extends BlockEntity implements MenuProvider, IThresholdsEnergy { + + private boolean EnergyDirty=true; + private int TickCount=0; + @Override + public int getEnergy() { + return ENERGY_STORAGE.getEnergy(); + } + protected final ItemStackHandler itemsHandler = new ItemStackHandler(1){ @Override protected void onContentsChanged(int slot) @@ -60,8 +69,7 @@ public class MagicalScrubberBlockEntity extends BlockEntity implements MenuProvi @Override public void onChanged() { setChanged(); - - ModMessages.sendToAll(new EnergySyncS2CPacket(energy, getBlockPos())); + EnergyDirty=true; } }; @@ -199,6 +207,16 @@ public class MagicalScrubberBlockEntity extends BlockEntity implements MenuProvi { if(lvl.isClientSide())return; + + if(entity.EnergyDirty) + { + if(entity.TickCount >= (2 * 20)) + { + ModMessages.sendToAll(new EnergySyncS2CPacket(entity.getEnergy(), pos)); + entity.EnergyDirty=false; + } else entity.TickCount++; + } + if(hasRecipe(entity)) { if(!hasEnergy(entity))return; // Halt until sufficient energy has been received @@ -326,6 +344,7 @@ public class MagicalScrubberBlockEntity extends BlockEntity implements MenuProvi return ENERGY_STORAGE; } + @Override public void setEnergy(int energy) { ENERGY_STORAGE.setEnergy(energy); } diff --git a/src/main/java/dev/zontreck/otemod/blocks/entity/UncrafterBlockEntity.java b/src/main/java/dev/zontreck/otemod/blocks/entity/UncrafterBlockEntity.java index 29e2408..9f8bef2 100644 --- a/src/main/java/dev/zontreck/otemod/blocks/entity/UncrafterBlockEntity.java +++ b/src/main/java/dev/zontreck/otemod/blocks/entity/UncrafterBlockEntity.java @@ -1,7 +1,10 @@ package dev.zontreck.otemod.blocks.entity; +import dev.zontreck.libzontreck.util.ItemUtils; import dev.zontreck.otemod.implementation.OutputItemStackHandler; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import dev.zontreck.otemod.implementation.energy.OTEEnergy; +import dev.zontreck.otemod.implementation.scrubber.MagicalScrubberMenu; import dev.zontreck.otemod.implementation.uncrafting.UncrafterMenu; import dev.zontreck.otemod.items.PartialItem; import dev.zontreck.otemod.networking.ModMessages; @@ -18,12 +21,15 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.ContainerData; import net.minecraft.world.inventory.CraftingContainer; +import net.minecraft.world.item.EnchantedBookItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.crafting.CraftingRecipe; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeType; +import net.minecraft.world.item.enchantment.Enchantment; +import net.minecraft.world.item.enchantment.EnchantmentInstance; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; @@ -34,37 +40,72 @@ import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemStackHandler; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import javax.annotation.Nullable; +import java.util.*; -public class UncrafterBlockEntity extends BlockEntity implements MenuProvider +public class UncrafterBlockEntity extends BlockEntity implements MenuProvider, IThresholdsEnergy { - public UncrafterBlockEntity(BlockPos position, BlockState state) { - super(ModEntities.UNCRAFTER.get(), position, state); - outputSlots = new OutputItemStackHandler(outputItems); + private boolean EnergyDirty=true; + private int TickCount=0; + + @Override + public int getEnergy() { + return ENERGY_STORAGE.getEnergy(); + } + + protected final ItemStackHandler itemsHandler = new ItemStackHandler(1){ + @Override + protected void onContentsChanged(int slot) + { + setChanged(); + } + }; + protected final ItemStackHandler outputItems = new ItemStackHandler(1){ + @Override + protected void onContentsChanged(int slot) + { + setChanged(); + } + }; + private ItemStackHandler outputSlot; + + private final OTEEnergy ENERGY_STORAGE = new OTEEnergy(ENERGY_REQ*3, ENERGY_REQ+512) { + + @Override + public void onChanged() { + setChanged(); + EnergyDirty=true; + } + + }; + + private static final int ENERGY_REQ = 250; + private LazyOptional lazyEnergyHandler = LazyOptional.empty(); + + private LazyOptional lazyItemHandler = LazyOptional.empty(); + private LazyOptional lazyOutputItems = LazyOptional.empty(); + + public UncrafterBlockEntity(BlockPos pos, BlockState state) { + super(ModEntities.UNCRAFTER.get(), pos, state); + outputSlot = new OutputItemStackHandler(outputItems); + this.data = new ContainerData() { + @Override - public int get(int i) { - switch (i) - { - case 0: { - return UncrafterBlockEntity.this.progress; - } - default: return 0; - } + public int get(int p_39284_) { + return switch(p_39284_){ + case 0 -> UncrafterBlockEntity.this.progress; + default -> 0; + }; } @Override - public void set(int i, int i1) { - switch (i) + public void set(int p_39285_, int p_39286_) { + switch(p_39285_) { - case 0: { - UncrafterBlockEntity.this.progress = i1; - } + case 0 -> UncrafterBlockEntity.this.progress = p_39286_; } } @@ -73,55 +114,45 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider public int getCount() { return 1; } + }; } protected final ContainerData data; - protected int progress = 0; + private int progress = 0; - private static final int ENERGY_REQUIREMENT = 250; + public static final int MAXIMUM_PROCESSING_TICKS = (3*20); // 3 seconds - public static int PROCESSING_TICKS = (3 * 20); // 3 seconds to uncraft - protected final ItemStackHandler itemHandler = new ItemStackHandler(1) { - @Override - protected void onContentsChanged(int slot) { - setChanged(); - } - }; - - protected final ItemStackHandler outputItems = new ItemStackHandler(1){ - @Override - protected void onContentsChanged(int slot) { - setChanged(); - } - }; - - private ItemStackHandler outputSlots; - private LazyOptional lazyEnergyHandler = LazyOptional.empty(); - - private LazyOptional lazyItemHandler = LazyOptional.empty(); - private LazyOptional lazyOutputItems = LazyOptional.empty(); - - private final OTEEnergy ENERGY_STORAGE = new OTEEnergy(ENERGY_REQUIREMENT * 10, ENERGY_REQUIREMENT*2) { - @Override - public void onChanged() { - - setChanged(); - - ModMessages.sendToAll(new EnergySyncS2CPacket(energy, getBlockPos())); - } - }; + @Override + @Nullable + public AbstractContainerMenu createMenu(int id, Inventory inv, Player player) { + return new UncrafterMenu(id, inv, this, this.data); + } @Override public Component getDisplayName() { - return Component.literal("Uncrafting Factory"); + return Component.translatable("block.otemod.uncrafter"); } - @Nullable @Override - public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { - return new UncrafterMenu(i, inventory, this, data); + public LazyOptional getCapability(Capability cap, Direction side) + { + if(cap == ForgeCapabilities.ENERGY) + { + return lazyEnergyHandler.cast(); + } + if(side == Direction.DOWN && cap == ForgeCapabilities.ITEM_HANDLER) + { + // Return the output slot only + return lazyOutputItems.cast(); + } + if(cap == ForgeCapabilities.ITEM_HANDLER) + { + return lazyItemHandler.cast(); + } + + return super.getCapability(cap,side); } @@ -129,8 +160,8 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider public void onLoad() { super.onLoad(); - lazyItemHandler = LazyOptional.of(()->itemHandler); - lazyOutputItems = LazyOptional.of(()->outputSlots); + lazyItemHandler = LazyOptional.of(()->itemsHandler); + lazyOutputItems = LazyOptional.of(()->outputSlot); lazyEnergyHandler = LazyOptional.of(()->ENERGY_STORAGE); } @@ -147,7 +178,7 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider @Override protected void saveAdditional(CompoundTag nbt) { - nbt.put("inventory", itemHandler.serializeNBT()); + nbt.put("inventory", itemsHandler.serializeNBT()); nbt.put("output", outputItems.serializeNBT()); nbt.putInt("prog", progress); nbt.putInt("energy", ENERGY_STORAGE.getEnergyStored()); @@ -159,32 +190,17 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider public void load(CompoundTag nbt){ super.load(nbt); - itemHandler.deserializeNBT(nbt.getCompound("inventory")); + itemsHandler.deserializeNBT(nbt.getCompound("inventory")); outputItems.deserializeNBT(nbt.getCompound("output")); progress = nbt.getInt("prog"); ENERGY_STORAGE.setEnergy(nbt.getInt("energy")); } - @Override - public @NotNull LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { - if(cap == ForgeCapabilities.ENERGY) // all sides accept power - { - return lazyEnergyHandler.cast(); - } - if(cap == ForgeCapabilities.FLUID_HANDLER) - { - //return lazyFluidHandler.cast(); // TODO: Implement a fluid storage, and add a spot for it on the GUI - } - if(cap == ForgeCapabilities.ITEM_HANDLER && side == Direction.DOWN) - { - return lazyOutputItems.cast(); - }else return lazyItemHandler.cast(); // all sides except bottom of block - } public void doDrop() { - SimpleContainer cont = new SimpleContainer(itemHandler.getSlots()); - for (int i = 0; i < itemHandler.getSlots(); i++) { - cont.setItem(i, itemHandler.getStackInSlot(i)); + SimpleContainer cont = new SimpleContainer(itemsHandler.getSlots()); + for (int i = 0; i < itemsHandler.getSlots(); i++) { + cont.setItem(i, itemsHandler.getStackInSlot(i)); } cont = new SimpleContainer(outputItems.getSlots()); for (int i = 0; i < outputItems.getSlots(); i++) { @@ -198,6 +214,14 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider { if(lvl.isClientSide())return; + if(entity.EnergyDirty) + { + if(entity.TickCount >= (2 * 20)) + { + ModMessages.sendToAll(new EnergySyncS2CPacket(entity.getEnergy(), pos)); + entity.EnergyDirty=false; + } else entity.TickCount++; + } if(hasRecipe(entity)) { @@ -206,9 +230,9 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider setChanged(lvl, pos, state); drain(entity); - if(entity.progress >= UncrafterBlockEntity.PROCESSING_TICKS) + if(entity.progress >= UncrafterBlockEntity.MAXIMUM_PROCESSING_TICKS) { - uncraftItem(entity); + craftItem(entity); } }else { if(entity.progress>0){ @@ -219,83 +243,25 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider } private static void drain(UncrafterBlockEntity entity) { - entity.ENERGY_STORAGE.extractEnergy(ENERGY_REQUIREMENT, false); + entity.ENERGY_STORAGE.extractEnergy(ENERGY_REQ, false); } private static boolean hasEnergy(UncrafterBlockEntity entity) { - return (entity.ENERGY_STORAGE.getEnergyStored() >= ENERGY_REQUIREMENT); + return (entity.ENERGY_STORAGE.getEnergyStored() >= ENERGY_REQ); } + private static void craftItem(UncrafterBlockEntity entity) { - private ItemStack[] getIngredients(Recipe recipe) { - ItemStack[] stacks = new ItemStack[recipe.getIngredients().size()]; - - for (int i = 0; i < recipe.getIngredients().size(); i++) { - ItemStack[] matchingStacks = Arrays.stream(recipe.getIngredients().get(i).getItems()).toArray(ItemStack[]::new); - - stacks[i] = matchingStacks.length > 0 ? matchingStacks[Math.floorMod(this.ingredientsInCycle, matchingStacks.length)] : ItemStack.EMPTY; - } - - - return stacks; - } - - private int ingredientsInCycle=0; - - - private static CraftingRecipe[] getRecipesFor(CraftingContainer matrix, Level world) { - return world.getRecipeManager().getRecipesFor(RecipeType.CRAFTING, matrix, world).toArray(new CraftingRecipe[0]); - } - - private static void uncraftItem(UncrafterBlockEntity entity) { if(hasRecipe(entity)) { - ItemStack existing = entity.outputItems.getStackInSlot(0); - List INGREDIENTS = new ArrayList<>(); - if(existing.getItem() instanceof PartialItem pi) - { - INGREDIENTS = PartialItem.getRemainingIngredients(existing); - - } else { - // Reverse recipe - - } - existing.setCount(existing.getCount()+1); - if(existing.is(Items.AIR)) - { - existing = makeOutputItems(entity.itemHandler.getStackInSlot(0)); - } - entity.itemHandler.extractItem(0, 1, false); - entity.outputItems.setStackInSlot(0, existing); - - entity.resetProgress(); } } - protected static ItemStack makeOutputItems(ItemStack original) - { - ItemStack newItem = new ItemStack(original.getItem(),1); - return newItem; - } - - private void resetProgress() { - progress=0; - - } - - public IEnergyStorage getEnergyStorage() { - return ENERGY_STORAGE; - } - - public void setEnergy(int energy) { - ENERGY_STORAGE.setEnergy(energy); - } - private static boolean hasRecipe(UncrafterBlockEntity entity) { - SimpleContainer inventory = new SimpleContainer(entity.itemHandler.getSlots()); - for(int i=0;i FLIGHT_ENCHANTMENT = REGISTERS.register("player_flight", ()->new FlightEnchantment(EquipmentSlot.FEET)); - public static final RegistryObject BORROWED_PROTECTION = REGISTERS.register("borrowed_protection", ()->new BorrowedProtectionEnchantment(Enchantment.Rarity.UNCOMMON, EnchantmentCategory.ARMOR, ARMOR_SLOTS)); - public static final RegistryObject CONSUMPTION_MENDING = REGISTERS.register("consumption_mending", ()->new ConsumptionMending(EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.FEET, EquipmentSlot.LEGS, EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND)); diff --git a/src/main/java/dev/zontreck/otemod/events/ClientEvents.java b/src/main/java/dev/zontreck/otemod/events/ClientEvents.java index f064d80..b4a381f 100644 --- a/src/main/java/dev/zontreck/otemod/events/ClientEvents.java +++ b/src/main/java/dev/zontreck/otemod/events/ClientEvents.java @@ -1,12 +1,22 @@ package dev.zontreck.otemod.events; +import dev.zontreck.libzontreck.vectors.Vector3; +import dev.zontreck.libzontreck.vectors.WorldPosition; import dev.zontreck.otemod.OTEMod; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer; import dev.zontreck.otemod.integrations.KeyBindings; import dev.zontreck.otemod.networking.ModMessages; +import dev.zontreck.otemod.networking.packets.EnergyRequestC2SPacket; import dev.zontreck.otemod.networking.packets.OpenVaultC2SPacket; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.event.InputEvent; +import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -29,8 +39,34 @@ public class ClientEvents { ModMessages.sendToServer(new OpenVaultC2SPacket(0, false, 0)); } } + + + private static int TICK_COUNT = 0; + @SubscribeEvent + public static void onClientTick(TickEvent.ClientTickEvent event) + { + TICK_COUNT ++; + + if(TICK_COUNT >= (2*20)) + { + Screen screen = Minecraft.getInstance().screen; + BlockPos pos; + BlockEntity entity; + Level lvl; + if(screen instanceof IThresholdsEnergyContainer itc) + { + pos = itc.getPosition(); + entity = itc.getEntity(); + lvl = entity.getLevel(); + ResourceLocation location = lvl.dimension().location(); + + ModMessages.sendToServer(new EnergyRequestC2SPacket(new WorldPosition(new Vector3(pos.getX(), pos.getY(), pos.getZ()), location.getNamespace() + ":" + location.getPath()), Minecraft.getInstance().player)); + } + } + } } + @Mod.EventBusSubscriber(modid = OTEMod.MOD_ID, value=Dist.CLIENT, bus=Mod.EventBusSubscriber.Bus.MOD) public static class ClientModBus { diff --git a/src/main/java/dev/zontreck/otemod/events/EventHandler.java b/src/main/java/dev/zontreck/otemod/events/EventHandler.java index fd2e0b1..a638576 100644 --- a/src/main/java/dev/zontreck/otemod/events/EventHandler.java +++ b/src/main/java/dev/zontreck/otemod/events/EventHandler.java @@ -241,6 +241,4 @@ public class EventHandler { ChatHelpers.broadcastTo(player, ChatHelpers.macro(Messages.OTE_PREFIX + " !Dark_Green!Your inventory has been saved for [0], and your [1] inventory has been restored", from.getName(), to.getName()), player.server); } - - } diff --git a/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberMenu.java b/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberMenu.java index 7a6d9d7..eee8be7 100644 --- a/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberMenu.java +++ b/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberMenu.java @@ -4,6 +4,7 @@ import dev.zontreck.otemod.blocks.ModBlocks; import dev.zontreck.otemod.blocks.entity.CompressionChamberBlockEntity; import dev.zontreck.otemod.blocks.entity.ItemScrubberBlockEntity; import dev.zontreck.otemod.blocks.entity.ModEntities; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer; import dev.zontreck.otemod.implementation.inits.ModMenuTypes; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -17,7 +18,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.items.SlotItemHandler; -public class CompressionChamberMenu extends AbstractContainerMenu +public class CompressionChamberMenu extends AbstractContainerMenu implements IThresholdsEnergyContainer { public final CompressionChamberBlockEntity entity; public final Level level; @@ -155,4 +156,14 @@ public class CompressionChamberMenu extends AbstractContainerMenu this.addSlot(new Slot(inv, index, PLAYER_INVENTORY_FIRST_SLOT_LEFT+index*18, PLAYER_HOTBAR_FIRST_SLOT)); } } + + @Override + public BlockPos getPosition() { + return entity.getBlockPos(); + } + + @Override + public BlockEntity getEntity() { + return entity; + } } diff --git a/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberScreen.java b/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberScreen.java index 4000a7e..ba0750e 100644 --- a/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberScreen.java +++ b/src/main/java/dev/zontreck/otemod/implementation/compressor/CompressionChamberScreen.java @@ -3,17 +3,21 @@ package dev.zontreck.otemod.implementation.compressor; import com.mojang.blaze3d.systems.RenderSystem; import dev.zontreck.otemod.OTEMod; import dev.zontreck.otemod.implementation.MouseHelpers; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer; import dev.zontreck.otemod.implementation.energy.screenrenderer.EnergyInfoArea; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.level.block.entity.BlockEntity; import java.util.Optional; -public class CompressionChamberScreen extends AbstractContainerScreen { +public class CompressionChamberScreen extends AbstractContainerScreen implements IThresholdsEnergyContainer +{ private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/energized_compression_chamber.png"); @@ -96,4 +100,14 @@ public class CompressionChamberScreen extends AbstractContainerScreen +public class ItemScrubberScreen extends AbstractContainerScreen implements IThresholdsEnergyContainer { private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/item_scrubber_gui.png"); @@ -96,5 +99,14 @@ public class ItemScrubberScreen extends AbstractContainerScreen +public class MagicalScrubberScreen extends AbstractContainerScreen implements IThresholdsEnergyContainer { private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/item_scrubber_gui.png"); @@ -97,4 +100,14 @@ public class MagicalScrubberScreen extends AbstractContainerScreen +public class UncrafterScreen extends AbstractContainerScreen implements IThresholdsEnergyContainer { private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/uncrafter.png"); @@ -95,4 +98,14 @@ public class UncrafterScreen extends AbstractContainerScreen { return MouseHelpers.isMouseOver(mouseX, mouseY, x+offsetX, y+offsetY, width, height); } + + @Override + public BlockPos getPosition() { + return menu.getPosition(); + } + + @Override + public BlockEntity getEntity() { + return menu.getEntity(); + } } diff --git a/src/main/java/dev/zontreck/otemod/networking/ModMessages.java b/src/main/java/dev/zontreck/otemod/networking/ModMessages.java index c24e11c..c4d4e04 100644 --- a/src/main/java/dev/zontreck/otemod/networking/ModMessages.java +++ b/src/main/java/dev/zontreck/otemod/networking/ModMessages.java @@ -1,6 +1,7 @@ package dev.zontreck.otemod.networking; import dev.zontreck.otemod.OTEMod; +import dev.zontreck.otemod.networking.packets.EnergyRequestC2SPacket; import dev.zontreck.otemod.networking.packets.EnergySyncS2CPacket; import dev.zontreck.otemod.networking.packets.OpenVaultC2SPacket; import net.minecraft.resources.ResourceLocation; @@ -40,6 +41,12 @@ public class ModMessages { .encoder(EnergySyncS2CPacket::toBytes) .consumerMainThread(EnergySyncS2CPacket::handle) .add(); + + net.messageBuilder(EnergyRequestC2SPacket.class, id(), NetworkDirection.PLAY_TO_SERVER) + .decoder(EnergyRequestC2SPacket::new) + .encoder(EnergyRequestC2SPacket::toBytes) + .consumerMainThread(EnergyRequestC2SPacket::handle) + .add(); } public static void sendToServer(MSG message){ diff --git a/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java b/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java new file mode 100644 index 0000000..60440b8 --- /dev/null +++ b/src/main/java/dev/zontreck/otemod/networking/packets/EnergyRequestC2SPacket.java @@ -0,0 +1,62 @@ +package dev.zontreck.otemod.networking.packets; + +import dev.zontreck.libzontreck.exceptions.InvalidDeserialization; +import dev.zontreck.libzontreck.util.ServerUtilities; +import dev.zontreck.libzontreck.vectors.WorldPosition; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; +import dev.zontreck.otemod.networking.ModMessages; +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.network.NetworkEvent; +import net.minecraftforge.server.ServerLifecycleHooks; + +import java.util.UUID; +import java.util.function.Supplier; + +public class EnergyRequestC2SPacket +{ + private WorldPosition position; + private UUID player; + + public EnergyRequestC2SPacket(WorldPosition position, Player player) + { + this.position=position; + this.player=player.getUUID(); + } + + public EnergyRequestC2SPacket(FriendlyByteBuf buf) + { + try { + position = new WorldPosition(buf.readAnySizeNbt(), false); + } catch (InvalidDeserialization e) { + throw new RuntimeException(e); + } + player = buf.readUUID(); + } + + public void toBytes(FriendlyByteBuf buf) + { + buf.writeNbt(position.serialize()); + buf.writeUUID(player); + } + + public void handle(Supplier ctx) + { + NetworkEvent.Context context = ctx.get(); + + context.enqueueWork(()-> + { + if(position==null)return; + BlockPos pos = position.Position.asBlockPos(); + BlockEntity entity = position.getActualDimension().getBlockEntity(pos); + if(entity instanceof IThresholdsEnergy ite) + { + int energy = ite.getEnergy(); + ModMessages.sendToPlayer(new EnergySyncS2CPacket(energy, pos), ServerUtilities.getPlayerByID(player.toString())); + } + }); + } + +} diff --git a/src/main/java/dev/zontreck/otemod/networking/packets/EnergySyncS2CPacket.java b/src/main/java/dev/zontreck/otemod/networking/packets/EnergySyncS2CPacket.java index 5b080d1..8c6999e 100644 --- a/src/main/java/dev/zontreck/otemod/networking/packets/EnergySyncS2CPacket.java +++ b/src/main/java/dev/zontreck/otemod/networking/packets/EnergySyncS2CPacket.java @@ -6,6 +6,7 @@ import dev.zontreck.otemod.blocks.entity.CompressionChamberBlockEntity; import dev.zontreck.otemod.blocks.entity.ItemScrubberBlockEntity; import dev.zontreck.otemod.blocks.entity.MagicalScrubberBlockEntity; import dev.zontreck.otemod.blocks.entity.UncrafterBlockEntity; +import dev.zontreck.otemod.implementation.energy.IThresholdsEnergy; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; @@ -40,16 +41,7 @@ public class EnergySyncS2CPacket { context.enqueueWork(()-> { // WE ARE NOW ON THE CLIENT - if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof ItemScrubberBlockEntity entity) - { - entity.setEnergy(energy); - } else if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof MagicalScrubberBlockEntity entity) - { - entity.setEnergy(energy); - } else if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof CompressionChamberBlockEntity entity) - { - entity.setEnergy(energy); - } else if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof UncrafterBlockEntity entity) + if(Minecraft.getInstance().level.getBlockEntity(pos) instanceof IThresholdsEnergy entity) { entity.setEnergy(energy); }