NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFieldCreator.java

215 lines
8.6 KiB
Java
Raw Normal View History

2018-11-13 00:36:47 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
2019-01-31 18:58:31 +01:00
import de.ellpeck.naturesaura.Helper;
2018-11-13 00:36:47 +01:00
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream;
2018-11-13 00:36:47 +01:00
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemFrameEntity;
2018-11-13 00:36:47 +01:00
import net.minecraft.item.ItemStack;
2020-01-23 20:57:56 +01:00
import net.minecraft.item.Items;
2020-09-22 03:17:02 +02:00
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.tileentity.ITickableBlockEntity;
import net.minecraft.tileentity.BlockEntity;
2018-11-13 00:36:47 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.math.vector.Vector3d;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.server.ServerLevel;
2018-11-13 00:36:47 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
2021-12-04 15:40:09 +01:00
import net.minecraftforge.event.level.BlockEvent;
2018-11-13 00:36:47 +01:00
2019-01-31 18:58:31 +01:00
import java.util.List;
2021-12-04 15:40:09 +01:00
public class BlockEntityFieldCreator extends BlockEntityImpl implements ITickableBlockEntity {
2018-11-13 00:36:47 +01:00
public BlockPos connectionOffset;
public boolean isMain;
public boolean isCharged;
private int chargeTimer;
2021-12-04 15:40:09 +01:00
public BlockEntityFieldCreator() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.FIELD_CREATOR);
2020-01-21 21:04:44 +01:00
}
2018-11-13 00:36:47 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide || this.level.getGameTime() % 10 != 0)
2018-11-13 00:36:47 +01:00
return;
BlockPos connectedPos = this.getConnectedPos();
2021-12-04 15:40:09 +01:00
if (connectedPos == null || !this.level.isBlockLoaded(connectedPos))
2018-11-13 00:36:47 +01:00
return;
2021-12-04 15:40:09 +01:00
BlockEntity other = this.level.getBlockEntity(connectedPos);
2018-11-13 00:36:47 +01:00
if (!this.isCloseEnough(connectedPos)
2021-12-04 15:40:09 +01:00
|| !(other instanceof BlockEntityFieldCreator)
|| !this.worldPosition.equals(((BlockEntityFieldCreator) other).getConnectedPos())) {
2018-11-13 00:36:47 +01:00
this.connectionOffset = null;
this.chargeTimer = 0;
this.isCharged = false;
this.isMain = false;
this.sendToClients();
return;
}
if (!this.isMain)
return;
2021-12-04 15:40:09 +01:00
BlockEntityFieldCreator creator = (BlockEntityFieldCreator) other;
if (this.redstonePower <= 0 && creator.redstonePower <= 0) {
2018-11-13 00:36:47 +01:00
this.chargeTimer = 0;
if (this.isCharged) {
this.isCharged = false;
this.sendToClients();
creator.isCharged = false;
creator.sendToClients();
}
return;
}
2021-12-04 15:40:09 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 32, this.worldPosition);
IAuraChunk chunk = IAuraChunk.getAuraChunk(this.level, spot);
2018-11-13 00:36:47 +01:00
if (!this.isCharged) {
this.chargeTimer += 10;
if (this.chargeTimer >= 150) {
this.chargeTimer = 0;
this.isCharged = true;
this.sendToClients();
creator.isCharged = true;
creator.sendToClients();
}
chunk.drainAura(spot, 300);
2018-11-13 00:36:47 +01:00
this.sendParticles();
} else {
2021-12-04 15:40:09 +01:00
if (this.level.getGameTime() % 40 == 0)
chunk.drainAura(spot, 20);
2018-11-13 00:36:47 +01:00
2020-01-23 20:57:56 +01:00
ItemStack tool = this.getToolUsed(creator);
2020-09-22 03:17:02 +02:00
Vector3d dist = new Vector3d(
2021-12-04 15:40:09 +01:00
this.worldPosition.getX() - connectedPos.getX(),
this.worldPosition.getY() - connectedPos.getY(),
this.worldPosition.getZ() - connectedPos.getZ()
2018-11-13 00:36:47 +01:00
);
double length = dist.length();
2020-09-22 03:17:02 +02:00
Vector3d normal = new Vector3d(dist.x / length, dist.y / length, dist.z / length);
2020-01-23 20:57:56 +01:00
for (float i = MathHelper.floor(length); i > 0; i -= 0.5F) {
2020-09-22 03:17:02 +02:00
Vector3d scaled = normal.scale(i);
2018-11-13 00:36:47 +01:00
BlockPos pos = connectedPos.add(
MathHelper.floor(scaled.x + 0.5F),
MathHelper.floor(scaled.y + 0.5F),
MathHelper.floor(scaled.z + 0.5F));
2021-12-04 15:40:09 +01:00
if (pos.equals(this.worldPosition) || pos.equals(connectedPos))
2018-11-13 00:36:47 +01:00
continue;
2021-12-04 15:40:09 +01:00
BlockState state = this.level.getBlockState(pos);
2018-11-13 00:36:47 +01:00
Block block = state.getBlock();
2021-12-04 15:40:09 +01:00
if (!block.isAir(state, this.level, pos) && state.getBlockHardness(this.level, pos) >= 0F) {
FakePlayer fake = FakePlayerFactory.getMinecraft((ServerLevel) this.level);
if (!MinecraftForge.EVENT_BUS.post(new BlockEvent.BreakEvent(this.level, pos, state, fake))) {
List<ItemStack> drops = state.getDrops(new LootContext.Builder((ServerLevel) this.level)
.withParameter(LootParameters.THIS_ENTITY, fake)
2020-09-22 03:17:02 +02:00
.withParameter(LootParameters.field_237457_g_, Vector3d.copyCentered(pos))
2020-01-23 20:57:56 +01:00
.withParameter(LootParameters.BLOCK_STATE, state)
2020-10-18 15:30:05 +02:00
.withParameter(LootParameters.TOOL, tool.isEmpty() ? new ItemStack(Items.DIAMOND_PICKAXE) : tool)
2021-12-04 15:40:09 +01:00
.withNullableParameter(LootParameters.BLOCK_ENTITY, this.level.getBlockEntity(pos)));
this.level.destroyBlock(pos, false);
2020-01-23 20:57:56 +01:00
for (ItemStack stack : drops)
2021-12-04 15:40:09 +01:00
Block.spawnAsEntity(this.level, pos, stack);
2020-10-18 15:30:51 +02:00
chunk.drainAura(spot, !tool.isEmpty() ? 300 : 100);
2020-01-23 20:57:56 +01:00
this.sendParticles();
2018-11-13 00:36:47 +01:00
}
}
}
}
}
2021-12-04 15:40:09 +01:00
private ItemStack getToolUsed(BlockEntityFieldCreator other) {
2020-01-23 20:57:56 +01:00
ItemStack myTool = this.getMyTool();
ItemStack otherTool = other.getMyTool();
if (!myTool.isEmpty()) {
// if both have tools, choose randomly
if (!otherTool.isEmpty())
2021-12-04 15:40:09 +01:00
return this.level.rand.nextBoolean() ? myTool : otherTool;
return myTool;
}
return otherTool;
2020-01-23 20:57:56 +01:00
}
private ItemStack getMyTool() {
2021-12-04 15:40:09 +01:00
List<ItemFrameEntity> frames = Helper.getAttachedItemFrames(this.level, this.worldPosition);
2019-10-20 22:30:49 +02:00
for (ItemFrameEntity frame : frames) {
2019-01-31 18:58:31 +01:00
ItemStack stack = frame.getDisplayedItem();
2020-01-23 20:57:56 +01:00
if (!stack.isEmpty())
return stack;
2019-01-31 18:58:31 +01:00
}
return ItemStack.EMPTY;
2019-01-31 18:58:31 +01:00
}
2018-11-13 00:36:47 +01:00
private void sendParticles() {
for (int j = 0; j < 2; j++) {
2021-12-04 15:40:09 +01:00
BlockPos p = j == 0 ? this.worldPosition : this.getConnectedPos();
PacketHandler.sendToAllAround(this.level, p, 32, new PacketParticleStream(
p.getX() + (float) this.level.rand.nextGaussian() * 3F,
p.getY() + 1 + this.level.rand.nextFloat() * 3F,
p.getZ() + (float) this.level.rand.nextGaussian() * 3F,
2018-11-13 01:27:47 +01:00
p.getX() + 0.5F,
p.getY() + 0.5F,
p.getZ() + 0.5F,
2021-12-04 15:40:09 +01:00
this.level.rand.nextFloat() * 0.07F + 0.07F, IAuraType.forLevel(this.level).getColor(), this.level.rand.nextFloat() + 0.5F
2020-01-22 23:21:52 +01:00
));
2018-11-13 00:36:47 +01:00
}
}
public boolean isCloseEnough(BlockPos pos) {
2020-01-24 17:05:41 +01:00
int range = ModConfig.instance.fieldCreatorRange.get() + 1;
2021-12-04 15:40:09 +01:00
return this.worldPosition.distanceSq(pos) <= range * range;
2018-11-13 00:36:47 +01:00
}
public BlockPos getConnectedPos() {
if (this.connectionOffset == null)
return null;
2021-12-04 15:40:09 +01:00
return this.worldPosition.add(this.connectionOffset);
2018-11-13 00:36:47 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-11-13 00:36:47 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
if (this.connectionOffset != null)
2020-01-21 21:04:44 +01:00
compound.putLong("connection", this.connectionOffset.toLong());
compound.putBoolean("main", this.isMain);
compound.putBoolean("charged", this.isCharged);
2018-11-13 00:36:47 +01:00
if (type == SaveType.TILE)
2020-01-21 21:04:44 +01:00
compound.putInt("timer", this.chargeTimer);
2018-11-13 00:36:47 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-11-13 00:36:47 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 21:04:44 +01:00
if (compound.contains("connection"))
2018-11-13 00:36:47 +01:00
this.connectionOffset = BlockPos.fromLong(compound.getLong("connection"));
else
this.connectionOffset = null;
this.isMain = compound.getBoolean("main");
this.isCharged = compound.getBoolean("charged");
if (type == SaveType.TILE)
2020-01-21 21:04:44 +01:00
this.chargeTimer = compound.getInt("timer");
2018-11-13 00:36:47 +01:00
}
}
}