Skip to content

Commit

Permalink
fix: electrotine generator not dropping contents
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed May 13, 2023
1 parent cf5d94d commit d72f5a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class ElectrotineGeneratorBlock extends ProjectRedBlock {

public ElectrotineGeneratorBlock() {
super(AbstractBlock.Properties.of(Material.STONE));
super(STONE_PROPERTIES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mrtjp.projectred.core.tile;

import codechicken.lib.util.ServerUtils;
import codechicken.lib.vec.Vector3;
import mrtjp.projectred.api.IConnectable;
import mrtjp.projectred.core.block.ProjectRedBlock;
import mrtjp.projectred.core.inventory.container.ElectrotineGeneratorContainer;
Expand All @@ -21,6 +22,8 @@
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -34,12 +37,8 @@ public class ElectrotineGeneratorTile extends BasePoweredTile implements ILowLoa

private int chargeFlow = 0;

private final Inventory inventory = new Inventory(1) {
@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return stack.getItem() == ELECTROTINE_DUST_ITEM;
}
};
private final ElectrotineGeneratorInventory inventory = new ElectrotineGeneratorInventory();
private final LazyOptional<? extends IItemHandler> handler = LazyOptional.of(() -> new InvWrapper(inventory));

private int burnTimeRemaining = 0;
private int powerStored = 0;
Expand Down Expand Up @@ -90,10 +89,10 @@ public ActionResultType onBlockActivated(PlayerEntity player, Hand hand, BlockRa
return ActionResultType.SUCCESS;
}

@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return super.getCapability(cap, side); //TODO add capabilities
public void onBlockRemoved() {
super.onBlockRemoved();
dropInventory(inventory, getLevel(), Vector3.fromBlockPos(getBlockPos()));
}

@Override
Expand Down Expand Up @@ -209,6 +208,23 @@ public boolean canConductorWork() {
return getConductorCharge() > 600;
}

//region Capabilities
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if (!this.remove && cap == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return handler.cast();
}
return super.getCapability(cap, side);
}

@Override
protected void invalidateCaps() {
super.invalidateCaps();
handler.invalidate();
}
//endregion

//region Container getters
public Inventory getInventory() {
return inventory;
Expand All @@ -223,4 +239,15 @@ public int getPowerStored() {
}
//endregion

private static class ElectrotineGeneratorInventory extends Inventory {

public ElectrotineGeneratorInventory() {
super(1);
}

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return stack.getItem() == ELECTROTINE_DUST_ITEM;
}
}
}

0 comments on commit d72f5a0

Please sign in to comment.