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

215 lines
8.5 KiB
Java
Raw Normal View History

2015-10-18 19:21:32 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityLaserRelay.java") is part of the Actually Additions mod for Minecraft.
2015-10-18 19:21:32 +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-10-18 19:21:32 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-10-18 19:21:32 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-10-18 19:21:32 +02:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-09-12 16:13:39 +02:00
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.ConnectionPair;
2017-02-13 19:07:53 +01:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import io.netty.util.internal.ConcurrentSet;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.Component;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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.items.IItemHandler;
2016-05-08 21:09:58 +02:00
2019-05-02 09:10:29 +02:00
public abstract class TileEntityLaserRelay extends TileEntityInventoryBase {
2015-10-18 19:21:32 +02:00
public static final int MAX_DISTANCE = 15;
2017-02-14 20:48:01 +01:00
public static final int MAX_DISTANCE_RANGED = 35;
2016-05-08 21:09:58 +02:00
public final LaserType type;
private Network cachedNetwork;
private int changeAmountAtCaching = -1;
2017-02-13 19:07:53 +01:00
private int lastRange;
2023-01-19 16:27:56 +01:00
// List of connections that are synced to the client for RenderLaserRelay to use
private final ConcurrentSet<IConnectionPair> connections = new ConcurrentSet<>();
2024-03-02 21:23:08 +01:00
public TileEntityLaserRelay(BlockEntityType<?> tileType, BlockPos pos, BlockState state, LaserType type) {
super(tileType, pos, state,1);
this.type = type;
}
2015-10-23 16:54:33 +02:00
@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.SYNC) {
ActuallyAdditionsAPI.connectionHandler.removeRelayFromNetwork(this.worldPosition, this.level);
2023-01-19 16:27:56 +01:00
this.connections.clear();
2024-03-02 21:23:08 +01:00
ListTag list = compound.getList("Connections", 10);
2019-05-02 09:10:29 +02:00
if (!list.isEmpty()) {
2021-02-26 22:15:48 +01:00
for (int i = 0; i < list.size(); i++) {
2016-09-12 16:13:39 +02:00
ConnectionPair pair = new ConnectionPair();
2023-01-19 16:27:56 +01:00
this.connections.add(pair);
2021-02-26 22:15:48 +01:00
pair.readFromNBT(list.getCompound(i));
ActuallyAdditionsAPI.connectionHandler.addConnection(pair.getPositions()[0], pair.getPositions()[1], this.type, this.level, pair.doesSuppressRender());
}
}
}
2015-10-23 16:54:33 +02:00
}
2015-10-18 19:21:32 +02:00
@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.SYNC) {
2024-03-02 21:23:08 +01:00
ListTag list = new ListTag();
ConcurrentSet<IConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.worldPosition, this.level);
2019-05-02 09:10:29 +02:00
if (connections != null && !connections.isEmpty()) {
for (IConnectionPair pair : connections) {
2024-03-02 21:23:08 +01:00
CompoundTag tag = new CompoundTag();
2016-09-12 16:13:39 +02:00
pair.writeToNBT(tag);
2021-02-27 16:33:00 +01:00
list.add(tag);
}
2015-10-21 00:22:50 +02:00
}
2021-02-27 16:33:00 +01:00
compound.put("Connections", list);
}
2015-10-18 19:21:32 +02:00
}
2017-02-13 19:07:53 +01:00
@Override
2024-03-02 21:23:08 +01:00
protected void clientTick() {
super.clientTick();
int range = this.getMaxRange();
if (this.lastRange != range) {
ConcurrentSet<IConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.worldPosition, this.level);
if (connections != null && !connections.isEmpty()) {
for (IConnectionPair pair : connections) {
int distanceSq = (int) pair.getPositions()[0].distSqr(pair.getPositions()[1]);
if (distanceSq > range * range) {
this.connections.remove(pair);
ActuallyAdditionsAPI.connectionHandler.removeConnection(this.level, pair.getPositions()[0], pair.getPositions()[1]);
}
}
}
2017-02-13 19:07:53 +01:00
2024-03-02 21:23:08 +01:00
this.lastRange = range;
}
}
@Override
protected void serverTick() {
super.serverTick();
2017-02-13 19:07:53 +01:00
int range = this.getMaxRange();
2019-05-02 09:10:29 +02:00
if (this.lastRange != range) {
ConcurrentSet<IConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.worldPosition, this.level);
2019-05-02 09:10:29 +02:00
if (connections != null && !connections.isEmpty()) {
for (IConnectionPair pair : connections) {
int distanceSq = (int) pair.getPositions()[0].distSqr(pair.getPositions()[1]);
2019-05-02 09:10:29 +02:00
if (distanceSq > range * range) {
2023-01-19 16:27:56 +01:00
this.connections.remove(pair);
ActuallyAdditionsAPI.connectionHandler.removeConnection(this.level, pair.getPositions()[0], pair.getPositions()[1]);
2017-02-13 19:07:53 +01:00
}
}
}
this.lastRange = range;
}
}
/*@Override
2016-02-01 17:49:55 +01:00
public void updateEntity(){
super.updateEntity();
2016-11-26 21:32:27 +01:00
if(this.world.isRemote){
2016-02-01 17:49:55 +01:00
this.renderParticles();
}
}
2019-05-02 10:43:27 +02:00
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2016-02-01 17:49:55 +01:00
public void renderParticles(){
2016-11-26 21:32:27 +01:00
if(this.world.rand.nextInt(8) == 0){
2021-02-26 22:15:48 +01:00
PlayerEntity player = Minecraft.getInstance().player;
if(player != null){
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
WrenchMode mode = WrenchMode.values()[data.theCompound.getInteger("LaserWrenchMode")];
if(mode != WrenchMode.NO_PARTICLES){
ItemStack stack = player.getHeldItemMainhand();
if(mode == WrenchMode.ALWAYS_PARTICLES || (StackUtil.isValid(stack) && stack.getItem() instanceof ItemLaserWrench)){
2016-11-26 21:32:27 +01:00
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.world);
if(network != null){
2016-09-12 16:13:39 +02:00
for(IConnectionPair aPair : network.connections){
if(!aPair.doesSuppressRender() && aPair.contains(this.pos) && this.pos.equals(aPair.getPositions()[0])){
2016-11-26 21:32:27 +01:00
AssetUtil.renderParticlesFromAToB(aPair.getPositions()[0].getX(), aPair.getPositions()[0].getY(), aPair.getPositions()[0].getZ(), aPair.getPositions()[1].getX(), aPair.getPositions()[1].getY(), aPair.getPositions()[1].getZ(), this.world.rand.nextInt(3)+1, 0.8F, this.type == LaserType.ITEM ? COLOR_ITEM : (this.type == LaserType.FLUID ? COLOR_FLUIDS : COLOR), 1F);
}
}
}
2016-02-01 17:49:55 +01:00
}
}
}
2015-10-21 00:22:50 +02:00
}
}*/
2015-10-18 19:21:32 +02:00
2019-05-02 09:10:29 +02:00
public Network getNetwork() {
if (this.cachedNetwork == null || this.cachedNetwork.changeAmount != this.changeAmountAtCaching) {
this.cachedNetwork = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.worldPosition, this.level);
2019-05-02 09:10:29 +02:00
if (this.cachedNetwork != null) {
this.changeAmountAtCaching = this.cachedNetwork.changeAmount;
2019-05-02 09:10:29 +02:00
} else {
this.changeAmountAtCaching = -1;
}
}
return this.cachedNetwork;
}
2024-03-04 20:21:48 +01:00
// @Override TODO: Fix the renderBoundingBox
// @OnlyIn(Dist.CLIENT)
// public AABB getRenderBoundingBox() {
// return INFINITE_EXTENT_AABB;
// }
2016-12-05 15:09:37 +01:00
2017-02-13 17:36:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldSyncSlots() {
2017-02-13 17:36:55 +01:00
return true;
}
@Override
2024-03-04 20:21:48 +01:00
public IItemHandler getItemHandler(Direction facing) {
return null;
2017-02-13 17:36:55 +01:00
}
2019-05-02 09:10:29 +02:00
public int getMaxRange() {
ItemStack upgrade = this.inv.getStackInSlot(0);
2021-05-02 18:10:21 +02:00
if (StackUtil.isValid(upgrade) && upgrade.getItem() == ActuallyItems.LASER_UPGRADE_RANGE.get()) {
2017-02-14 20:48:01 +01:00
return MAX_DISTANCE_RANGED;
2019-05-02 09:10:29 +02:00
} else {
2017-02-13 19:07:53 +01:00
return MAX_DISTANCE;
}
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public abstract Component getExtraDisplayString();
2016-12-05 15:09:37 +01:00
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public abstract Component getCompassDisplayString();
2016-12-05 15:09:37 +01:00
2024-03-02 21:23:08 +01:00
public abstract void onCompassAction(Player player);
2023-01-19 16:27:56 +01:00
public ConcurrentSet<IConnectionPair> getConnections() {
return connections;
}
2015-10-18 19:21:32 +02:00
}