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

226 lines
7.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityPhantomface.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-20 22:39:43 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.BlockPhantom;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.capabilities.ICapabilityInvalidationListener;
2015-05-20 22:39:43 +02:00
2019-05-02 09:10:29 +02:00
public abstract class TileEntityPhantomface extends TileEntityInventoryBase implements IPhantomTile {
2015-12-01 19:48:09 +01:00
public static final int RANGE = 16;
2016-01-08 13:31:58 +01:00
public BlockPos boundPosition;
public BlockPhantom.Type type;
public int range;
private int rangeBefore;
2016-01-08 13:31:58 +01:00
private BlockPos boundPosBefore;
private Block boundBlockBefore;
private int lastStrength;
private CapListener capListener = new CapListener(this);
2024-03-02 21:23:08 +01:00
public TileEntityPhantomface(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state, 0);
2015-05-20 22:39:43 +02:00
}
2024-03-02 21:23:08 +01:00
public static int upgradeRange(int defaultRange, Level world, BlockPos pos) {
2016-02-01 20:39:11 +01:00
int newRange = defaultRange;
2019-05-02 09:10:29 +02:00
for (int i = 0; i < 3; i++) {
Block block = world.getBlockState(pos.above(1 + i)).getBlock();
2021-05-02 17:47:50 +02:00
if (block == ActuallyBlocks.PHANTOM_BOOSTER.get()) {
2019-05-02 09:10:29 +02:00
newRange = newRange * 2;
} else {
2016-02-01 20:39:11 +01:00
break;
}
}
2016-02-01 20:39:11 +01:00
return newRange;
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("Range", this.range);
2019-05-02 09:10:29 +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());
}
}
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
2019-05-02 09:10:29 +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");
2019-05-02 09:10:29 +02:00
if (!(x == 0 && y == 0 && z == 0)) {
this.boundPosition = new BlockPos(x, y, z);
this.setChanged();
}
}
}
2015-05-27 21:57:53 +02:00
@Override
2024-03-02 21:23:08 +01:00
protected void serverTick() {
super.serverTick();
this.range = upgradeRange(RANGE, this.level, this.getBlockPos());
2015-05-27 21:57:53 +02:00
2024-03-02 21:23:08 +01:00
if (!this.hasBoundPosition()) {
this.boundPosition = null;
}
2024-03-02 21:23:08 +01:00
if (this.doesNeedUpdateSend()) {
this.onUpdateSent();
if (level instanceof ServerLevel serverLevel) {
capListener.disable();
capListener = new CapListener(this);
if (hasBoundPosition())
serverLevel.registerCapabilityListener(boundPosition, capListener);
}
2024-03-02 21:23:08 +01:00
}
2024-03-02 21:23:08 +01:00
int strength = this.getComparatorStrength();
if (this.lastStrength != strength) {
this.lastStrength = strength;
2024-03-02 21:23:08 +01:00
this.setChanged();
}
}
@Override
protected void clientTick() {
super.clientTick();
if (this.boundPosition != null) {
this.renderParticles();
2015-10-26 22:47:26 +01:00
}
2015-05-27 21:57:53 +02:00
}
2019-05-02 09:10:29 +02:00
protected boolean doesNeedUpdateSend() {
return this.boundPosition != this.boundPosBefore || this.boundPosition != null && this.level.getBlockState(this.boundPosition).getBlock() != this.boundBlockBefore || this.rangeBefore != this.range;
}
2019-05-02 09:10:29 +02:00
protected void onUpdateSent() {
this.rangeBefore = this.range;
this.boundPosBefore = this.boundPosition;
2021-02-26 22:15:48 +01:00
this.boundBlockBefore = this.boundPosition == null
? null
: this.level.getBlockState(this.boundPosition).getBlock();
2019-05-02 09:10:29 +02:00
if (this.boundPosition != null) {
this.level.updateNeighborsAt(this.worldPosition, this.level.getBlockState(this.boundPosition).getBlock());
}
this.sendUpdate();
this.setChanged();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean hasBoundPosition() {
if (this.boundPosition != null) {
if (this.level.getBlockEntity(this.boundPosition) instanceof IPhantomTile || this.getBlockPos().getX() == this.boundPosition.getX() && this.getBlockPos().getY() == this.boundPosition.getY() && this.getBlockPos().getZ() == this.boundPosition.getZ()) {
this.boundPosition = null;
return false;
}
return true;
}
return false;
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public void renderParticles() {
if (this.level.random.nextInt(2) == 0) {
double d1 = this.boundPosition.getY() + this.level.random.nextFloat();
int i1 = this.level.random.nextInt(2) * 2 - 1;
int j1 = this.level.random.nextInt(2) * 2 - 1;
double d4 = (this.level.random.nextFloat() - 0.5D) * 0.125D;
2019-05-02 09:10:29 +02:00
double d2 = this.boundPosition.getZ() + 0.5D + 0.25D * j1;
double d5 = this.level.random.nextFloat() * 1.0F * j1;
2019-05-02 09:10:29 +02:00
double d0 = this.boundPosition.getX() + 0.5D + 0.25D * i1;
double d3 = this.level.random.nextFloat() * 1.0F * i1;
this.level.addParticle(ParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
2015-10-28 14:46:04 +01:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public boolean isBoundThingInRange() {
return this.hasBoundPosition() && this.boundPosition.distSqr(this.getBlockPos()) <= this.range * this.range;
}
@Override
2019-05-02 09:10:29 +02:00
public BlockPos getBoundPosition() {
return this.boundPosition;
}
2015-10-03 10:16:18 +02:00
@Override
2019-05-02 09:10:29 +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
}
2021-11-21 17:31:57 +01:00
//@Override
2019-05-02 09:10:29 +02:00
public int getGuiID() {
return -1;
}
@Override
2019-05-02 09:10:29 +02:00
public int getRange() {
return this.range;
}
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
if (this.isBoundThingInRange()) {
BlockPos pos = this.getBoundPosition();
BlockState state = this.level.getBlockState(pos);
if (state.hasAnalogOutputSignal()) {
return state.getAnalogOutputSignal(this.level, pos);
2021-02-26 22:15:48 +01:00
}
}
return 0;
}
public class CapListener implements ICapabilityInvalidationListener {
private boolean valid = true;
private final TileEntityPhantomface tile;
public CapListener(TileEntityPhantomface tile) {
this.tile = tile;
}
public void disable() {
valid = false;
}
@Override
public boolean onInvalidate() {
if (valid && !tile.isRemoved()) {
tile.invalidateCapabilities();
return true;
}
return false;
}
}
2015-05-20 22:39:43 +02:00
}