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

247 lines
8.8 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityPhantomPlacer.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-05-25 17:00:54 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerPhantomPlacer;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler;
2016-07-06 20:05:56 +02:00
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
2019-05-02 09:10:29 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-25 17:00:54 +02:00
import net.minecraft.block.Block;
2021-02-27 16:33:00 +01:00
import net.minecraft.block.Blocks;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
2015-05-25 17:00:54 +02:00
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2021-02-27 16:33:00 +01:00
import net.minecraft.particles.ParticleTypes;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2021-02-27 16:33:00 +01:00
import net.minecraft.world.server.ServerWorld;
import javax.annotation.Nullable;
2021-02-27 16:33:00 +01:00
import java.util.List;
2015-05-25 17:00:54 +02:00
public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements IPhantomTile, IButtonReactor, INamedContainerProvider {
2015-05-25 17:00:54 +02:00
2015-12-01 19:48:09 +01:00
public static final int RANGE = 3;
2016-01-08 13:31:58 +01:00
public BlockPos boundPosition;
2015-05-25 17:00:54 +02:00
public int currentTime;
public int range;
2015-05-25 17:00:54 +02:00
public boolean isBreaker;
2016-07-06 20:05:56 +02:00
public int side;
2016-07-07 17:59:45 +02:00
private int oldRange;
2015-05-25 17:00:54 +02:00
2021-02-27 16:33:00 +01:00
public TileEntityPhantomPlacer(TileEntityType<?> type, int slots) {
super(type, slots);
2015-05-25 17:00:54 +02:00
}
2018-08-10 05:04:07 +02:00
public TileEntityPhantomPlacer() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.PHANTOMPLACER_TILE.get(), 9);
2015-05-25 17:00:54 +02:00
this.isBreaker = false;
}
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("Range", this.range);
2018-08-10 05:04:07 +02:00
if (this.boundPosition != null) {
2021-02-27 16:33:00 +01:00
compound.putInt("xOfTileStored", this.boundPosition.getX());
compound.putInt("yOfTileStored", this.boundPosition.getY());
compound.putInt("zOfTileStored", this.boundPosition.getZ());
}
2018-08-10 05:04:07 +02:00
if (!this.isBreaker) {
2021-02-27 16:33:00 +01:00
compound.putInt("Side", this.side);
2016-07-06 20:05:56 +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) {
super.readSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
int x = compound.getInt("xOfTileStored");
int y = compound.getInt("yOfTileStored");
int z = compound.getInt("zOfTileStored");
this.range = compound.getInt("Range");
2018-08-10 05:04:07 +02:00
if (!(x == 0 && y == 0 && z == 0)) {
this.boundPosition = new BlockPos(x, y, z);
this.markDirty();
}
2018-08-10 05:04:07 +02:00
if (!this.isBreaker) {
2021-02-27 16:33:00 +01:00
this.side = compound.getInt("Side");
2016-07-06 20:05:56 +02:00
}
2016-02-01 17:49:55 +01:00
}
}
2015-05-25 17:00:54 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void updateEntity() {
super.updateEntity();
2018-08-10 05:04:07 +02:00
if (!this.world.isRemote) {
2016-11-26 21:32:27 +01:00
this.range = TileEntityPhantomface.upgradeRange(RANGE, this.world, this.pos);
2015-05-25 17:00:54 +02:00
2018-08-10 05:04:07 +02:00
if (!this.hasBoundPosition()) {
2015-05-25 17:00:54 +02:00
this.boundPosition = null;
}
2018-08-10 05:04:07 +02:00
if (this.isBoundThingInRange()) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
2015-05-25 17:00:54 +02:00
this.currentTime--;
2018-08-10 05:04:07 +02:00
if (this.currentTime <= 0) {
this.doWork();
2015-05-25 17:00:54 +02:00
}
2018-08-10 05:04:07 +02:00
} else {
this.currentTime = 30;
2015-10-02 16:48:01 +02:00
}
2015-05-25 17:00:54 +02:00
}
}
2018-08-10 05:04:07 +02:00
if (this.oldRange != this.range) {
this.oldRange = this.range;
this.sendUpdate();
}
2018-08-10 05:04:07 +02:00
} else {
if (this.boundPosition != null) {
2015-12-16 20:10:27 +01:00
this.renderParticles();
}
}
}
2021-02-27 16:33:00 +01:00
// TODO: [port] I have no clue what the cowboy logic is trying to do here. Confirm this still works
2015-12-19 10:30:39 +01:00
@Override
2018-08-10 05:04:07 +02:00
public boolean hasBoundPosition() {
if (this.boundPosition != null) {
2021-02-27 16:33:00 +01:00
if (this.world.getTileEntity(this.boundPosition) instanceof IPhantomTile || this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ() && this.world.getDimensionType() == this.world.getDimensionType()) {
2015-12-19 10:30:39 +01:00
this.boundPosition = null;
return false;
}
2021-02-27 16:33:00 +01:00
return this.world.getDimensionType() == this.world.getDimensionType();
2015-12-19 10:30:39 +01:00
}
return false;
}
2018-08-10 05:04:07 +02:00
private void doWork() {
if (this.isBoundThingInRange()) {
if (this.isBreaker) {
2016-11-26 21:32:27 +01:00
Block blockToBreak = this.world.getBlockState(this.boundPosition).getBlock();
2018-08-10 05:04:07 +02:00
if (blockToBreak != null && this.world.getBlockState(this.boundPosition).getBlockHardness(this.world, this.boundPosition) > -1.0F) {
2021-02-27 16:33:00 +01:00
List<ItemStack> drops = Block.getDrops(this.world.getBlockState(this.boundPosition), (ServerWorld) this.world, this.pos, this.world.getTileEntity(this.pos));
2018-08-10 05:04:07 +02:00
if (StackUtil.canAddAll(this.inv, drops, false)) {
2016-11-26 21:32:27 +01:00
this.world.playEvent(2001, this.boundPosition, Block.getStateId(this.world.getBlockState(this.boundPosition)));
2021-02-27 16:33:00 +01:00
this.world.setBlockState(this.boundPosition, Blocks.AIR.getDefaultState());
StackUtil.addAll(this.inv, drops, false);
this.markDirty();
2016-01-23 11:02:35 +01:00
}
2015-12-19 10:30:39 +01:00
}
2018-08-10 05:04:07 +02:00
} else {
int theSlot = StackUtil.findFirstFilled(this.inv);
2021-02-26 22:15:48 +01:00
if (theSlot == -1) {
return;
}
2019-02-27 19:53:05 +01:00
this.inv.setStackInSlot(theSlot, WorldUtil.useItemAtSide(WorldUtil.getDirectionBySidesInOrder(this.side), this.world, this.boundPosition, this.inv.getStackInSlot(theSlot)));
2015-12-19 10:30:39 +01:00
}
}
}
2018-08-10 05:04:07 +02:00
public void renderParticles() {
if (this.world.rand.nextInt(2) == 0) {
2019-02-27 19:53:05 +01:00
double d1 = this.boundPosition.getY() + this.world.rand.nextFloat();
2018-08-10 05:04:07 +02:00
int i1 = this.world.rand.nextInt(2) * 2 - 1;
int j1 = this.world.rand.nextInt(2) * 2 - 1;
2019-02-27 19:53:05 +01:00
double d4 = (this.world.rand.nextFloat() - 0.5D) * 0.125D;
double d2 = this.boundPosition.getZ() + 0.5D + 0.25D * j1;
double d5 = this.world.rand.nextFloat() * 1.0F * j1;
double d0 = this.boundPosition.getX() + 0.5D + 0.25D * i1;
double d3 = this.world.rand.nextFloat() * 1.0F * i1;
2021-02-27 16:33:00 +01:00
this.world.addParticle(ParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
2015-12-16 20:10:27 +01:00
}
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public boolean isBoundThingInRange() {
return this.hasBoundPosition() && this.boundPosition.distanceSq(this.pos) <= this.range * this.range;
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public BlockPos getBoundPosition() {
return this.boundPosition;
}
2015-10-03 10:16:18 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void setBoundPosition(BlockPos pos) {
2016-07-04 20:15:41 +02:00
this.boundPosition = pos;
2015-10-03 10:16:18 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public int getGuiID() {
return GuiHandler.GuiTypes.PHANTOM_PLACER.ordinal();
}
@Override
2018-08-10 05:04:07 +02:00
public int getRange() {
return this.range;
2015-05-25 17:00:54 +02:00
}
2015-10-18 15:31:01 +02:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return ItemStackHandlerAA.ACCEPT_TRUE;
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
2018-10-28 18:38:26 +01:00
return (slot, automation) -> !automation;
2015-05-25 17:00:54 +02:00
}
2015-10-03 10:16:18 +02:00
@Override
2018-08-10 05:04:07 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2018-08-10 05:04:07 +02:00
public void activateOnPulse() {
this.doWork();
}
2016-07-06 20:05:56 +02:00
@Override
2021-02-26 22:15:48 +01:00
public void onButtonPressed(int buttonID, PlayerEntity player) {
if (this.side + 1 >= Direction.values().length) {
2016-07-06 20:05:56 +02:00
this.side = 0;
2018-08-10 05:04:07 +02:00
} else {
2016-07-06 20:05:56 +02:00
this.side++;
}
this.sendUpdate();
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
return new ContainerPhantomPlacer(windowId, playerInventory, this);
}
2015-05-25 17:00:54 +02:00
}