ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFishingNet.java

95 lines
3.7 KiB
Java
Raw Normal View History

2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-03-30 15:08:19 +02:00
2019-05-02 09:10:29 +02:00
import java.util.List;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2015-03-30 15:08:19 +02:00
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
2015-05-07 16:36:29 +02:00
import net.minecraft.item.ItemStack;
2015-03-30 15:08:19 +02:00
import net.minecraft.nbt.NBTTagCompound;
2015-05-07 16:36:29 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.world.WorldServer;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootTableList;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
2015-03-30 15:08:19 +02:00
2019-05-02 09:10:29 +02:00
public class TileEntityFishingNet extends TileEntityBase {
2015-03-30 15:08:19 +02:00
public int timeUntilNextDrop;
2019-05-02 09:10:29 +02:00
public TileEntityFishingNet() {
super("fishingNet");
}
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("TimeUntilNextDrop", this.timeUntilNextDrop);
}
2016-02-01 17:49:55 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
this.timeUntilNextDrop = compound.getInteger("TimeUntilNextDrop");
}
2016-02-01 17:49:55 +01:00
}
2015-03-30 15:08:19 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (!this.isRedstonePowered) {
if (this.world.getBlockState(this.pos.down()).getMaterial() == Material.WATER) {
if (this.timeUntilNextDrop > 0) {
2015-05-07 16:36:29 +02:00
this.timeUntilNextDrop--;
2019-05-02 09:10:29 +02:00
if (this.timeUntilNextDrop <= 0) {
LootContext.Builder builder = new LootContext.Builder((WorldServer) this.world);
2016-11-26 21:32:27 +01:00
List<ItemStack> fishables = this.world.getLootTableManager().getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING).generateLootForPools(this.world.rand, builder.build());
2019-05-02 09:10:29 +02:00
for (ItemStack fishable : fishables) {
ItemStack leftover = this.storeInContainer(fishable);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(leftover)) {
EntityItem item = new EntityItem(this.world, this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5, leftover.copy());
2016-03-18 23:47:22 +01:00
item.lifespan = 2000;
2016-11-26 21:32:27 +01:00
this.world.spawnEntity(item);
2016-03-18 23:47:22 +01:00
}
2015-05-07 16:36:29 +02:00
}
}
2019-05-02 09:10:29 +02:00
} else {
int time = 15000;
2019-05-02 09:10:29 +02:00
this.timeUntilNextDrop = time + this.world.rand.nextInt(time / 2);
2015-10-02 16:48:01 +02:00
}
2015-05-07 16:36:29 +02:00
}
}
}
}
2019-05-02 09:10:29 +02:00
private ItemStack storeInContainer(ItemStack stack) {
for (EnumFacing side : EnumFacing.values()) {
TileEntity tile = this.tilesAround[side.ordinal()];
2019-05-02 09:10:29 +02:00
if (tile != null) {
if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())) {
IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
2019-05-02 09:10:29 +02:00
if (cap != null) {
for (int i = 0; i < cap.getSlots(); i++) {
stack = cap.insertItem(i, stack, false);
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(stack)) { return StackUtil.getEmpty(); }
}
}
}
}
}
return stack;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldSaveDataOnChangeOrWorldStart() {
return true;
}
2015-03-30 15:08:19 +02:00
}