Throttle Energy Sync packet to when energy is changed, on delayed tick, and only when screen is opened on the player.

This commit is contained in:
Zontreck 2024-03-04 16:33:36 -07:00
parent eed092a4a7
commit 724abc6dbd
25 changed files with 466 additions and 226 deletions

View file

@ -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

View file

@ -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);
}
}
}

View file

@ -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<Block, BlockState> 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<Block, BlockState> 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;
}

View file

@ -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();
}
}

View file

@ -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<IEnergyStorage> lazyEnergyHandler = LazyOptional.empty();
private LazyOptional<IItemHandler> 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);
}

View file

@ -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);
}

View file

@ -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<IEnergyStorage> lazyEnergyHandler = LazyOptional.empty();
private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
private LazyOptional<IItemHandler> 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<IEnergyStorage> lazyEnergyHandler = LazyOptional.empty();
private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
private LazyOptional<IItemHandler> 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 <T> LazyOptional<T> getCapability(Capability<T> 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 <T> LazyOptional<T> getCapability(@NotNull Capability<T> 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<Item> 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<entity.itemHandler.getSlots();i++)
SimpleContainer inventory = new SimpleContainer(entity.itemsHandler.getSlots());
for(int i=0;i<entity.itemsHandler.getSlots();i++)
{
inventory.setItem(i, entity.itemHandler.getStackInSlot(i));
inventory.setItem(i, entity.itemsHandler.getStackInSlot(i));
}
SimpleContainer output = new SimpleContainer(entity.outputItems.getSlots());
for(int i=0;i<entity.outputItems.getSlots();i++)
@ -304,12 +270,12 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider
}
boolean hasAnItem = !entity.itemHandler.getStackInSlot(0).isEmpty();
boolean hasAnItem = !entity.itemsHandler.getStackInSlot(0).isEmpty();
ItemStack result = null;
if(hasAnItem)
{
result = makeOutputItems(entity.itemHandler.getStackInSlot(0));
result = makeOutputItem(entity.itemsHandler.getStackInSlot(0));
}
return hasAnItem && canInsertIntoOutput(output, result);
@ -324,4 +290,24 @@ public class UncrafterBlockEntity extends BlockEntity implements MenuProvider
if(outputEmpty)return true;
return (stackCompat && sameType);
}
private void resetProgress() {
progress=0;
}
protected static ItemStack makeOutputItem(ItemStack original)
{
ItemStack newItem = new ItemStack(original.getItem(),1);
return newItem;
}
public IEnergyStorage getEnergyStorage() {
return ENERGY_STORAGE;
}
@Override
public void setEnergy(int energy) {
ENERGY_STORAGE.setEnergy(energy);
}
}

View file

@ -1,33 +0,0 @@
package dev.zontreck.otemod.enchantments;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
public class BorrowedProtectionEnchantment extends Enchantment
{
protected BorrowedProtectionEnchantment(Rarity pRarity, EnchantmentCategory pCategory, EquipmentSlot[] pApplicableSlots) {
super(pRarity, pCategory, pApplicableSlots);
}
@Override
public boolean isCurse() {
return false;
}
@Override
public boolean isTradeable() {
return true;
}
@Override
public boolean isDiscoverable() {
return true;
}
@Override
public boolean isTreasureOnly() {
return false;
}
}

View file

@ -18,8 +18,6 @@ public class ModEnchantments {
public static final RegistryObject<Enchantment> FLIGHT_ENCHANTMENT = REGISTERS.register("player_flight", ()->new FlightEnchantment(EquipmentSlot.FEET));
public static final RegistryObject<Enchantment> BORROWED_PROTECTION = REGISTERS.register("borrowed_protection", ()->new BorrowedProtectionEnchantment(Enchantment.Rarity.UNCOMMON, EnchantmentCategory.ARMOR, ARMOR_SLOTS));
public static final RegistryObject<Enchantment> CONSUMPTION_MENDING = REGISTERS.register("consumption_mending", ()->new ConsumptionMending(EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.FEET, EquipmentSlot.LEGS, EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND));

View file

@ -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
{

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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<CompressionChamberMenu> {
public class CompressionChamberScreen extends AbstractContainerScreen<CompressionChamberMenu> 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<Compressio
{
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();
}
}

View file

@ -0,0 +1,6 @@
package dev.zontreck.otemod.implementation.energy;
public interface IThresholdsEnergy {
int getEnergy();
void setEnergy(int energy);
}

View file

@ -0,0 +1,13 @@
package dev.zontreck.otemod.implementation.energy;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
/**
* This is used to mark a screen as a energy container to automatically send the energy request packets
*/
public interface IThresholdsEnergyContainer
{
BlockPos getPosition();
BlockEntity getEntity();
}

View file

@ -39,4 +39,9 @@ public abstract class OTEEnergy extends EnergyStorage
public abstract void onChanged();
public int getEnergy()
{
return energy;
}
}

View file

@ -2,7 +2,9 @@ package dev.zontreck.otemod.implementation.scrubber;
import dev.zontreck.otemod.blocks.ModBlocks;
import dev.zontreck.otemod.blocks.entity.ItemScrubberBlockEntity;
import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
@ -14,7 +16,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.items.SlotItemHandler;
public class ItemScrubberMenu extends AbstractContainerMenu
public class ItemScrubberMenu extends AbstractContainerMenu implements IThresholdsEnergyContainer
{
public final ItemScrubberBlockEntity entity;
private final Level level;
@ -147,4 +149,13 @@ public class ItemScrubberMenu extends AbstractContainerMenu
}
}
@Override
public BlockPos getPosition() {
return entity.getBlockPos();
}
@Override
public BlockEntity getEntity() {
return entity;
}
}

View file

@ -7,15 +7,18 @@ import com.mojang.blaze3d.vertex.PoseStack;
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;
public class ItemScrubberScreen extends AbstractContainerScreen<ItemScrubberMenu>
public class ItemScrubberScreen extends AbstractContainerScreen<ItemScrubberMenu> implements IThresholdsEnergyContainer
{
private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/item_scrubber_gui.png");
@ -97,4 +100,13 @@ public class ItemScrubberScreen extends AbstractContainerScreen<ItemScrubberMenu
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();
}
}

View file

@ -2,7 +2,9 @@ package dev.zontreck.otemod.implementation.scrubber;
import dev.zontreck.otemod.blocks.ModBlocks;
import dev.zontreck.otemod.blocks.entity.MagicalScrubberBlockEntity;
import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
@ -14,7 +16,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.items.SlotItemHandler;
public class MagicalScrubberMenu extends AbstractContainerMenu
public class MagicalScrubberMenu extends AbstractContainerMenu implements IThresholdsEnergyContainer
{
public final MagicalScrubberBlockEntity entity;
private final Level level;
@ -147,4 +149,13 @@ public class MagicalScrubberMenu extends AbstractContainerMenu
}
}
@Override
public BlockPos getPosition() {
return entity.getBlockPos();
}
@Override
public BlockEntity getEntity() {
return entity;
}
}

View file

@ -7,15 +7,18 @@ import com.mojang.blaze3d.vertex.PoseStack;
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;
public class MagicalScrubberScreen extends AbstractContainerScreen<MagicalScrubberMenu>
public class MagicalScrubberScreen extends AbstractContainerScreen<MagicalScrubberMenu> 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<MagicalScrubb
{
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();
}
}

View file

@ -3,7 +3,9 @@ package dev.zontreck.otemod.implementation.uncrafting;
import dev.zontreck.otemod.blocks.ModBlocks;
import dev.zontreck.otemod.blocks.entity.CompressionChamberBlockEntity;
import dev.zontreck.otemod.blocks.entity.UncrafterBlockEntity;
import dev.zontreck.otemod.implementation.energy.IThresholdsEnergyContainer;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
@ -15,7 +17,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.items.SlotItemHandler;
public class UncrafterMenu extends AbstractContainerMenu
public class UncrafterMenu extends AbstractContainerMenu implements IThresholdsEnergyContainer
{
public final UncrafterBlockEntity entity;
@ -62,7 +64,7 @@ public class UncrafterMenu extends AbstractContainerMenu
{
if(!isCrafting())return 0;
int progress = this.data.get(0);
int max = UncrafterBlockEntity.PROCESSING_TICKS;
int max = UncrafterBlockEntity.MAXIMUM_PROCESSING_TICKS;
int progressArrow = 69;
@ -152,4 +154,14 @@ public class UncrafterMenu 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;
}
}

View file

@ -4,17 +4,20 @@ import com.mojang.blaze3d.systems.RenderSystem;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.implementation.MouseHelpers;
import dev.zontreck.otemod.implementation.compressor.CompressionChamberMenu;
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 UncrafterScreen extends AbstractContainerScreen<UncrafterMenu>
public class UncrafterScreen extends AbstractContainerScreen<UncrafterMenu> 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<UncrafterMenu>
{
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();
}
}

View file

@ -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 <MSG> void sendToServer(MSG message){

View file

@ -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<NetworkEvent.Context> 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()));
}
});
}
}

View file

@ -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);
}