Jump to content

CosmicKid

Members
  • Posts

    9
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

CosmicKid's Achievements

Tree Puncher

Tree Puncher (2/8)

0

Reputation

  1. I've looked at plenty of other people's code as well as the Minecraft code for containers like this and I can't figure this out. When I shift+click the iron ore in my inventory, it fills up the slot in my furnace, but the stack size in my inventory stays what it was if the entire stack couldn't be moved to the furnace. My slots are set up like this: 0-26 is the inner inventory, 27-35 is the hotbar, 36 is the furnace input and 37 is the output. @Override public ItemStack quickMoveStack(PlayerEntity player, int index) { ItemStack itemstack = ItemStack.EMPTY; Slot slot = this.slots.get(index); if(slot != null && slot.hasItem()) { ItemStack stack = slot.getItem(); itemstack = stack.copy(); if(index == 36) { if(!this.moveItemStackTo(stack, 0, 36, true)) { return ItemStack.EMPTY; } slot.onQuickCraft(stack, itemstack); } else { RecipeManager recipeManager = player.level.getRecipeManager(); Inventory input = new Inventory(stack); Optional<FurnaceRecipe> matchingRecipe = recipeManager.getRecipeFor(IRecipeType.SMELTING, input, player.level); if(index == 37) { if(!this.moveItemStackTo(stack, 27, 35, false)) { if(!this.moveItemStackTo(stack, 0, 26, false)) { return ItemStack.EMPTY; } } } else if(matchingRecipe.isPresent()) { if(!this.moveItemStackTo(stack, 36, 37, true)) { if(!this.moveItemStackTo(stack, index, index, false)) { if(!this.moveItemStackTo(stack, 27, 35, false)) { if(!this.moveItemStackTo(stack, 0, 26, false)) { return ItemStack.EMPTY; } } } } } else if(index < 27) { if(!this.moveItemStackTo(stack, 27, 35, false)) { return ItemStack.EMPTY; } } else if(index < 36 && !this.moveItemStackTo(stack, 0, 26, false)) { return ItemStack.EMPTY; } } if(stack.isEmpty()) { slot.set(ItemStack.EMPTY); } else { slot.setChanged(); } if(stack.getCount() == itemstack.getCount()) { return ItemStack.EMPTY; } slot.onTake(player, stack); } return itemstack; }
  2. I don't know what I was doing wrong before, but the deserializing works now. Thanks for your help!
  3. Ohhhh, I see. I’ll try it again with the built-in methods, but they weren’t deserializing correctly; they were giving me blank values. It makes sense that it was throwing an error from me feeding the ResourceLocation incorrectly, though.
  4. So what is the fix exactly? Are you saying that load isn’t getting called because I’m not using the handlers’ deserialize methods?
  5. Because they weren’t loading properly back when load was actually called. It was giving me a blank list of length 1 and 0 for the energy.
  6. https://github.com/CameronPersonett/Noble Note that my ElectricSmelterTileEntity, which extends from AbstractMachineTileEntity, has its load method called perfectly fine. The CombustionEngineTileEntity however, does not. This is weird, because they're practically the same implementation at the moment.
  7. AbstractEngineTileEntity: public abstract class AbstractEngineTileEntity extends TileEntity implements ITickableTileEntity { protected int counter; protected CustomEnergyStorage energyStorage = createEnergy(); protected LazyOptional<IEnergyStorage> energy = LazyOptional.of(() -> energyStorage); public AbstractEngineTileEntity(TileEntityType type) { super(type); } private CustomEnergyStorage createEnergy() { return new CustomEnergyStorage(Config.COMBUSTION_ENGINE_MAX_POWER, 0) { @Override protected void onEnergyChanged() { setChanged(); } }; } @Override public void load(BlockState state, CompoundNBT tag) { super.load(state, tag); } @Override public CompoundNBT save(CompoundNBT tag) { return super.save(tag); } @Override public CompoundNBT getUpdateTag() { return this.save(new CompoundNBT()); } @Override public SUpdateTileEntityPacket getUpdatePacket() { CompoundNBT nbt = new CompoundNBT(); this.save(nbt); return new SUpdateTileEntityPacket(this.getBlockPos(), 0, nbt); } @Override public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket packet) { this.load(level.getBlockEntity(packet.getPos()).getBlockState(), packet.getTag()); } @Override public abstract void tick(); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) { if (cap == CapabilityEnergy.ENERGY) { return energy.cast(); } return super.getCapability(cap, side); } } CombustionEngineTileEntity: public class CombustionEngineTileEntity extends AbstractEngineTileEntity { protected ItemStackHandler itemHandler = createHandler(); protected LazyOptional<IItemHandler> handler = LazyOptional.of(() -> itemHandler); public CombustionEngineTileEntity() { super(Registration.COMBUSTION_ENGINE_TILE_ENTITY.get()); } @Override public void setRemoved() { super.setRemoved(); handler.invalidate(); energy.invalidate(); } @Override public void tick() { if (level.isClientSide) { return; } if (counter > 0) { counter--; if (counter <= 0) { energyStorage.addEnergy(Config.COMBUSTION_ENGINE_GENERATION); } setChanged(); } if (counter <= 0) { ItemStack stack = itemHandler.getStackInSlot(0); if (stack.getItem() == Items.DIAMOND) { itemHandler.extractItem(0, 1, false); counter = Config.COMBUSTION_ENGINE_TICKS; setChanged(); } } BlockState blockState = level.getBlockState(getBlockPos()); if (blockState.getValue(BlockStateProperties.POWERED) != counter > 0) { level.setBlock(getBlockPos(), blockState.setValue(BlockStateProperties.POWERED, counter > 0), Constants.BlockFlags.NOTIFY_NEIGHBORS + Constants.BlockFlags.BLOCK_UPDATE); } sendOutPower(); } private void sendOutPower() { AtomicInteger capacity = new AtomicInteger(energyStorage.getEnergyStored()); if (capacity.get() > 0) { for (Direction direction : Direction.values()) { TileEntity te = level.getBlockEntity(getBlockPos().offset(new Vector3i(direction.getStepX(), direction.getStepY(), direction.getStepZ()))); if (te != null) { boolean doContinue = te.getCapability(CapabilityEnergy.ENERGY, direction).map(energy -> { if (energy.canReceive()) { int received = energy.receiveEnergy(Math.min(capacity.get(), Config.COMBUSTION_ENGINE_SEND), false); capacity.addAndGet(-received); energyStorage.consumeEnergy(received); setChanged(); return capacity.get() > 0; } else { return true; } }).orElse(true); if (!doContinue) { return; } } } } } @Override public void load(BlockState state, CompoundNBT tag) { CompoundNBT inv = tag.getCompound("inv"); CompoundNBT item = inv.getCompound("item"); itemHandler.setStackInSlot(0, new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation(item.getString("name"))), item.getInt("qty"))); energyStorage.setEnergy(tag.getInt("eng")); counter = tag.getInt("counter"); super.load(state, tag); } @Override public CompoundNBT save(CompoundNBT tag) { CompoundNBT item = new CompoundNBT(); item.putString("name", itemHandler.getStackInSlot(0).getDisplayName().getString()); item.putInt("qty", itemHandler.getStackInSlot(0).getCount()); CompoundNBT inv = new CompoundNBT(); inv.put("item", item); tag.put("inv", inv); tag.putInt("eng", energyStorage.getEnergyStored()); tag.putInt("counter", counter); return super.save(tag); } private ItemStackHandler createHandler() { return new ItemStackHandler(1) { @Override protected void onContentsChanged(int slot) { setChanged(); } @Override public boolean isItemValid(int slot, @Nonnull ItemStack stack) { return stack.getItem() == Items.DIAMOND; } @Override public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { if (stack.getItem() != Items.DIAMOND) { return stack; } return super.insertItem(slot, stack, simulate); } }; } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) { if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return handler.cast(); } return super.getCapability(cap, side); } } Registration: public class Registration { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Noble.MOD_ID); public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, Noble.MOD_ID); public static final DeferredRegister<EntityType<?>> ENTITIES = DeferredRegister.create(ForgeRegistries.ENTITIES, Noble.MOD_ID); public static final DeferredRegister<TileEntityType<?>> TILE_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, Noble.MOD_ID); public static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, Noble.MOD_ID); public static void init() { BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus()); ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus()); ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus()); TILE_ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus()); CONTAINERS.register(FMLJavaModLoadingContext.get().getModEventBus()); } public static final RegistryObject<CombustionEngineBlock> COMBUSTION_ENGINE_BLOCK = BLOCKS.register("combustion_engine", CombustionEngineBlock::new); public static final RegistryObject<TileEntityType<CombustionEngineTileEntity>> COMBUSTION_ENGINE_TILE_ENTITY = TILE_ENTITIES.register("combustion_engine", () -> TileEntityType.Builder.of(CombustionEngineTileEntity::new, COMBUSTION_ENGINE_BLOCK.get()).build(null)); public static final RegistryObject<ContainerType<AbstractEngineContainer>> COMBUSTION_ENGINE_CONTAINER = CONTAINERS.register("combustion_engine", () -> IForgeContainerType.create(CombustionEngineContainer::new)); }
  8. public void render(MatrixStack stack, int mouseX, int mouseY, float partialTicks) { renderBg(stack, partialTicks, mouseX, mouseY); renderSlots(stack); renderCursorSlot(stack, mouseX, mouseY); renderLabels(stack, mouseX, mouseY); renderTooltip(stack, mouseX, mouseY); } public void renderSlots(MatrixStack stack) { int x = (this.width - this.imageWidth) / 2; int y = (this.height - this.imageHeight) / 2; for(Slot slot : container.slots) { super.itemRenderer.renderGuiItem(slot.getItem(), slot.x + x, slot.y + y); if(!slot.getItem().isEmpty() && !(slot.getItem().getCount() == 1)) { this.font.draw(stack, Integer.toString(slot.getItem().getCount()), slot.x + x + 8, slot.y + y + 8, 0xffffff); } } } For some reason, it seems like the item renderer goes after the font draw even though it's called before. Any idea what's going on?
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.