2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityFishingNet.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-03-30 15:08:19 +02:00
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2015-03-30 15:08:19 +02:00
|
|
|
import net.minecraft.block.material.Material;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
2015-05-07 16:36:29 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.loot.LootContext;
|
|
|
|
import net.minecraft.loot.LootParameterSets;
|
|
|
|
import net.minecraft.loot.LootTables;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2015-05-07 16:36:29 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2016-10-19 16:28:09 +02:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
2015-03-30 15:08:19 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
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() {
|
2021-02-27 13:24:45 +01:00
|
|
|
super(ActuallyTiles.FISHINGNET_TILE.get());
|
2016-05-06 23:23:29 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
compound.putInt("TimeUntilNextDrop", this.timeUntilNextDrop);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
this.timeUntilNextDrop = compound.getInt("TimeUntilNextDrop");
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
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() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2021-02-27 13:24:45 +01:00
|
|
|
|
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) {
|
2021-02-27 13:24:45 +01:00
|
|
|
// TODO: [port] come back to this as the loot table may be wrong
|
|
|
|
LootContext.Builder builder = new LootContext.Builder((ServerWorld) this.world);
|
|
|
|
List<ItemStack> fishables = this.world.getServer().getLootTableManager().getLootTableFromLocation(LootTables.GAMEPLAY_FISHING).generate(builder.build(LootParameterSets.FISHING));
|
2019-05-02 09:10:29 +02:00
|
|
|
for (ItemStack fishable : fishables) {
|
2016-10-19 16:28:09 +02:00
|
|
|
ItemStack leftover = this.storeInContainer(fishable);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(leftover)) {
|
2021-02-27 13:24:45 +01:00
|
|
|
ItemEntity item = new ItemEntity(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;
|
2021-02-27 13:24:45 +01:00
|
|
|
this.world.addEntity(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 {
|
2016-10-19 18:24:50 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 16:28:09 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private ItemStack storeInContainer(ItemStack stack) {
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction side : Direction.values()) {
|
2016-10-19 16:28:09 +02:00
|
|
|
TileEntity tile = this.tilesAround[side.ordinal()];
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null) {
|
2021-02-27 13:24:45 +01:00
|
|
|
// TODO: [port] come back and make sure this works
|
|
|
|
ItemStack copyStack = stack.copy();
|
|
|
|
stack = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()).map(cap -> {
|
|
|
|
ItemStack localStack = ItemStack.EMPTY;
|
|
|
|
for (int i = 0; i < cap.getSlots(); i++) {
|
|
|
|
localStack = cap.insertItem(i, copyStack, false);
|
2016-10-19 16:28:09 +02:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
if (!StackUtil.isValid(localStack)) {
|
|
|
|
return ItemStack.EMPTY;
|
2016-10-19 16:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-27 13:24:45 +01:00
|
|
|
return localStack;
|
|
|
|
}).orElse(ItemStack.EMPTY);
|
2016-10-19 16:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart() {
|
2016-10-19 16:28:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-30 15:08:19 +02:00
|
|
|
}
|