2018-11-05 16:36:10 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2018-11-09 12:42:08 +01:00
|
|
|
import de.ellpeck.naturesaura.items.ModItems;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.entity.item.ItemFrameEntity;
|
2018-11-05 16:36:10 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2020-01-23 20:57:56 +01:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.tileentity.ITickableTileEntity;
|
2018-11-05 16:36:10 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Direction;
|
|
|
|
import net.minecraft.util.Hand;
|
2018-11-05 16:36:10 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-01-23 20:57:56 +01:00
|
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2020-01-23 20:57:56 +01:00
|
|
|
import net.minecraftforge.common.ForgeHooks;
|
2018-11-05 16:36:10 +01:00
|
|
|
import net.minecraftforge.common.util.FakePlayer;
|
|
|
|
import net.minecraftforge.common.util.FakePlayerFactory;
|
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
public class TileEntityPlacer extends TileEntityImpl implements ITickableTileEntity {
|
|
|
|
|
2020-01-22 01:32:26 +01:00
|
|
|
public TileEntityPlacer() {
|
|
|
|
super(ModTileEntities.PLACER);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
2018-11-05 16:36:10 +01:00
|
|
|
|
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick() {
|
|
|
|
if (!this.world.isRemote && this.world.getGameTime() % 15 == 0) {
|
2018-11-28 13:18:21 +01:00
|
|
|
if (this.redstonePower > 0)
|
|
|
|
return;
|
2018-11-05 16:36:10 +01:00
|
|
|
TileEntity tileUp = this.world.getTileEntity(this.pos.up());
|
2020-01-21 21:04:44 +01:00
|
|
|
if (tileUp == null)
|
2018-11-05 16:36:10 +01:00
|
|
|
return;
|
2020-01-21 21:04:44 +01:00
|
|
|
IItemHandler handler = tileUp.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.DOWN).orElse(null);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (handler == null)
|
|
|
|
return;
|
2019-10-20 22:30:49 +02:00
|
|
|
List<ItemFrameEntity> frames = Helper.getAttachedItemFrames(this.world, this.pos);
|
2018-11-05 16:36:10 +01:00
|
|
|
if (frames.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<BlockPos> validPositions = new ArrayList<>();
|
|
|
|
int range = 5;
|
|
|
|
for (int x = -range; x <= range; x++)
|
|
|
|
for (int y = -range; y <= range; y++)
|
|
|
|
for (int z = -range; z <= range; z++) {
|
|
|
|
BlockPos pos = this.pos.add(x, y, z);
|
|
|
|
if (!this.framesContain(frames, pos, this.world.getBlockState(pos)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BlockPos up = pos.up();
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState state = this.world.getBlockState(up);
|
2020-01-23 22:40:03 +01:00
|
|
|
if (state.getMaterial().isReplaceable())
|
2018-11-05 16:36:10 +01:00
|
|
|
validPositions.add(up);
|
|
|
|
}
|
|
|
|
if (validPositions.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 0; i < handler.getSlots(); i++) {
|
|
|
|
ItemStack stack = handler.extractItem(i, 1, true);
|
|
|
|
if (stack.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BlockPos pos = validPositions.get(this.world.rand.nextInt(validPositions.size()));
|
|
|
|
ItemStack left = this.tryPlace(stack.copy(), pos);
|
|
|
|
if (ItemStack.areItemStacksEqual(stack, left))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handler.extractItem(i, 1, false);
|
2018-11-11 13:26:19 +01:00
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 10, this.pos);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, 1000);
|
2018-11-05 18:03:07 +01:00
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(pos.getX(), pos.getY(), pos.getZ(), 9));
|
2018-11-05 18:03:07 +01:00
|
|
|
|
2018-11-05 16:36:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
private boolean framesContain(List<ItemFrameEntity> frames, BlockPos pos, BlockState state) {
|
2018-11-05 16:36:10 +01:00
|
|
|
ItemStack stack = state.getBlock().getItem(this.world, pos, state);
|
|
|
|
if (stack.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
for (ItemFrameEntity frame : frames) {
|
2018-11-05 16:36:10 +01:00
|
|
|
ItemStack frameStack = frame.getDisplayedItem();
|
2018-11-09 12:42:08 +01:00
|
|
|
if (frameStack.isEmpty())
|
|
|
|
continue;
|
2018-11-20 11:48:45 +01:00
|
|
|
if (Helper.areItemsEqual(stack, frameStack, false))
|
2018-11-09 12:42:08 +01:00
|
|
|
return true;
|
|
|
|
|
2018-11-20 11:48:45 +01:00
|
|
|
if (state.getBlock() == Blocks.FARMLAND && frameStack.getItem() == ModItems.FARMING_STENCIL)
|
2018-11-05 16:36:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemStack tryPlace(ItemStack stack, BlockPos pos) {
|
2019-10-20 22:30:49 +02:00
|
|
|
if (!(this.world instanceof ServerWorld))
|
2018-11-05 16:36:10 +01:00
|
|
|
return stack;
|
2019-10-20 22:30:49 +02:00
|
|
|
FakePlayer fake = FakePlayerFactory.getMinecraft((ServerWorld) this.world);
|
2018-11-05 16:36:10 +01:00
|
|
|
fake.inventory.mainInventory.set(fake.inventory.currentItem, stack);
|
2020-01-23 20:57:56 +01:00
|
|
|
BlockRayTraceResult ray = new BlockRayTraceResult(new Vec3d(pos).add(0.5F, 0.5F, 0.5F), Direction.UP, pos, false);
|
|
|
|
ForgeHooks.onPlaceItemIntoWorld(new ItemUseContext(fake, Hand.MAIN_HAND, ray));
|
2018-11-05 16:36:10 +01:00
|
|
|
return fake.getHeldItemMainhand().copy();
|
|
|
|
}
|
|
|
|
}
|