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

233 lines
8.2 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityPhantomPlacer.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 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;
2016-01-23 11:02:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.Util;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-25 17:00:54 +02:00
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2016-01-08 13:31:58 +01:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Vec3;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-05-25 17:00:54 +02:00
import java.util.ArrayList;
public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements IPhantomTile, IRedstoneToggle{
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;
2015-12-19 10:30:39 +01:00
private boolean activateOnceWithSignal;
private int oldRange;
2015-05-25 17:00:54 +02:00
public TileEntityPhantomPlacer(int slots, String name){
super(slots, name);
}
public TileEntityPhantomPlacer(){
super(9, "phantomPlacer");
this.isBreaker = false;
}
@Override
public void updateEntity(){
super.updateEntity();
2015-05-25 17:00:54 +02:00
if(!worldObj.isRemote){
2016-01-08 13:31:58 +01:00
this.range = TileEntityPhantomface.upgradeRange(RANGE, worldObj, this.pos);
2015-05-25 17:00:54 +02:00
if(!this.hasBoundPosition()){
this.boundPosition = null;
}
if(this.isBoundThingInRange()){
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
2015-05-25 17:00:54 +02:00
if(this.currentTime > 0){
this.currentTime--;
if(this.currentTime <= 0){
this.doWork();
2015-05-25 17:00:54 +02:00
}
}
2015-10-02 16:48:01 +02:00
else{
this.currentTime = 30;
2015-10-02 16:48:01 +02:00
}
2015-05-25 17:00:54 +02:00
}
}
if(this.oldRange != this.range){
this.oldRange = this.range;
this.sendUpdate();
}
2015-05-25 17:00:54 +02:00
}
2015-12-16 20:10:27 +01:00
else{
if(this.boundPosition != null){
this.renderParticles();
}
}
}
2015-12-19 10:30:39 +01:00
@Override
public boolean hasBoundPosition(){
if(this.boundPosition != null){
if(this.worldObj.getTileEntity(boundPosition) instanceof IPhantomTile || (this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ() && this.worldObj.provider.getDimensionId() == this.worldObj.provider.getDimensionId())){
2015-12-19 10:30:39 +01:00
this.boundPosition = null;
return false;
}
return this.worldObj.provider.getDimensionId() == this.worldObj.provider.getDimensionId();
2015-12-19 10:30:39 +01:00
}
return false;
}
private void doWork(){
if(this.isBreaker){
2016-01-08 13:31:58 +01:00
Block blockToBreak = PosUtil.getBlock(boundPosition, worldObj);
if(blockToBreak != null && blockToBreak.getBlockHardness(worldObj, boundPosition) > -1.0F){
2015-12-19 10:30:39 +01:00
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
2016-01-08 13:31:58 +01:00
drops.addAll(blockToBreak.getDrops(worldObj, boundPosition, worldObj.getBlockState(boundPosition), 0));
2015-12-19 10:30:39 +01:00
if(WorldUtil.addToInventory(this, drops, false, true)){
2016-01-23 11:02:35 +01:00
if(!ConfigValues.lessBlockBreakingEffects){
2016-01-23 23:29:19 +01:00
worldObj.playAuxSFX(2001, this.boundPosition, Block.getStateId(worldObj.getBlockState(this.boundPosition)));
2016-01-23 11:02:35 +01:00
}
worldObj.setBlockToAir(this.boundPosition);
WorldUtil.addToInventory(this, drops, true, true);
2015-12-19 10:30:39 +01:00
this.markDirty();
}
}
}
else{
int theSlot = WorldUtil.findFirstFilledSlot(this.slots);
this.setInventorySlotContents(theSlot, WorldUtil.useItemAtSide(EnumFacing.UP, worldObj, boundPosition, this.slots[theSlot]));
if(this.slots[theSlot] != null && this.slots[theSlot].stackSize <= 0){
this.slots[theSlot] = null;
2015-12-19 10:30:39 +01:00
}
}
}
2015-12-16 20:10:27 +01:00
@SideOnly(Side.CLIENT)
public void renderParticles(){
if(Util.RANDOM.nextInt(2) == 0){
double d1 = (double)((float)this.boundPosition.getY()+Util.RANDOM.nextFloat());
int i1 = Util.RANDOM.nextInt(2)*2-1;
int j1 = Util.RANDOM.nextInt(2)*2-1;
double d4 = ((double)Util.RANDOM.nextFloat()-0.5D)*0.125D;
double d2 = (double)this.boundPosition.getZ()+0.5D+0.25D*(double)j1;
double d5 = (double)(Util.RANDOM.nextFloat()*1.0F*(float)j1);
double d0 = (double)this.boundPosition.getX()+0.5D+0.25D*(double)i1;
double d3 = (double)(Util.RANDOM.nextFloat()*1.0F*(float)i1);
worldObj.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
2015-12-16 20:10:27 +01:00
}
if(this.ticksElapsed%80 == 0){
PacketParticle.renderParticlesFromAToB(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), boundPosition.getX(), boundPosition.getY(), boundPosition.getZ(), 2, 0.35F, TileEntityPhantomface.COLORS, 3);
}
2015-05-25 17:00:54 +02:00
}
@Override
public boolean isBoundThingInRange(){
2016-01-08 13:31:58 +01:00
return this.hasBoundPosition() && PosUtil.toVec(this.boundPosition).distanceTo(new Vec3(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ())) <= this.range;
2015-05-25 17:00:54 +02:00
}
@Override
2016-01-08 13:31:58 +01:00
public BlockPos getBoundPosition(){
return this.boundPosition;
}
2015-10-03 10:16:18 +02:00
@Override
2016-01-08 13:31:58 +01:00
public void setBoundPosition(BlockPos pos){
this.boundPosition = pos == null ? null : PosUtil.copyPos(pos);
2015-10-03 10:16:18 +02:00
}
@Override
public int getGuiID(){
return GuiHandler.GuiTypes.PHANTOM_PLACER.ordinal();
}
@Override
public int getRange(){
return this.range;
2015-05-25 17:00:54 +02:00
}
2015-10-18 15:31:01 +02:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
compound.setInteger("Range", this.range);
if(this.boundPosition != null){
2015-10-18 15:31:01 +02:00
compound.setInteger("XCoordOfTileStored", boundPosition.getX());
compound.setInteger("YCoordOfTileStored", boundPosition.getY());
compound.setInteger("ZCoordOfTileStored", boundPosition.getZ());
}
}
2015-05-25 17:00:54 +02:00
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
2015-05-25 17:00:54 +02:00
int x = compound.getInteger("XCoordOfTileStored");
int y = compound.getInteger("YCoordOfTileStored");
int z = compound.getInteger("ZCoordOfTileStored");
this.range = compound.getInteger("Range");
if(!(x == 0 && y == 0 && z == 0)){
2016-01-08 13:31:58 +01:00
this.boundPosition = new BlockPos(x, y, z);
this.markDirty();
2015-05-25 17:00:54 +02:00
}
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
2015-12-19 10:30:39 +01:00
return this.isItemValidForSlot(slot, stack);
2015-05-25 17:00:54 +02:00
}
@Override
2015-12-19 10:30:39 +01:00
public boolean isItemValidForSlot(int i, ItemStack stack){
return !this.isBreaker;
2015-05-25 17:00:54 +02:00
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
2015-05-30 17:47:57 +02:00
return this.isBreaker;
2015-05-25 17:00:54 +02:00
}
2015-10-03 10:16:18 +02:00
@Override
public void toggle(boolean to){
this.activateOnceWithSignal = to;
}
@Override
public boolean isPulseMode(){
return this.activateOnceWithSignal;
}
@Override
public void activateOnPulse(){
this.doWork();
}
2015-10-03 10:16:18 +02:00
public static class TileEntityPhantomBreaker extends TileEntityPhantomPlacer{
public TileEntityPhantomBreaker(){
super(9, "phantomBreaker");
this.isBreaker = true;
}
}
2015-05-25 17:00:54 +02:00
}