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

122 lines
5.1 KiB
Java
Raw Normal View History

2019-02-08 22:36:44 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.items.ModItems;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.network.protocol.game.ClientboundSetTimePacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraft.world.phys.AABB;
2019-02-08 22:36:44 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEntityTimeChanger extends BlockEntityImpl implements ITickableBlockEntity {
2019-02-08 22:36:44 +01:00
private long goalTime;
2021-12-08 00:31:29 +01:00
public BlockEntityTimeChanger(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.TIME_CHANGER, pos, state);
2020-01-21 21:04:44 +01:00
}
2019-02-08 22:36:44 +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) {
2021-12-15 16:30:22 +01:00
var frames = Helper.getAttachedItemFrames(this.level, this.worldPosition);
for (var frame : frames) {
var frameStack = frame.getItem();
2019-02-08 22:36:44 +01:00
if (frameStack.isEmpty() || frameStack.getItem() != ModItems.CLOCK_HAND)
continue;
if (this.goalTime > 0) {
2021-12-15 16:30:22 +01:00
var current = this.level.getDayTime();
var toAdd = Math.min(75, this.goalTime - current);
2019-02-08 22:36:44 +01:00
if (toAdd <= 0) {
this.goalTime = 0;
this.sendToClients();
return;
}
2021-12-08 00:31:29 +01:00
((ServerLevelData) this.level.getLevelData()).setDayTime(current + toAdd);
2019-02-08 22:36:44 +01:00
2021-12-15 16:30:22 +01:00
var spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
2021-12-04 15:40:09 +01:00
IAuraChunk.getAuraChunk(this.level, spot).drainAura(spot, (int) toAdd * 20);
2019-02-08 22:36:44 +01:00
2021-12-04 15:40:09 +01:00
if (this.level instanceof ServerLevel) {
2021-12-15 16:30:22 +01:00
var list = this.level.getServer().getPlayerList();
2021-12-08 00:31:29 +01:00
list.broadcastAll(new ClientboundSetTimePacket(
2021-12-04 15:40:09 +01:00
this.level.getGameTime(), this.level.getDayTime(),
2021-12-08 00:31:29 +01:00
this.level.getGameRules().getBoolean(GameRules.RULE_DAYLIGHT)));
2019-02-08 22:36:44 +01:00
}
return;
}
2021-12-04 15:40:09 +01:00
if (this.level.getGameTime() % 20 != 0)
2019-02-08 22:36:44 +01:00
return;
2021-12-15 16:30:22 +01:00
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(this.worldPosition).inflate(1), Entity::isAlive);
for (var item : items) {
2021-12-08 00:31:29 +01:00
if (item.hasPickUpDelay())
2019-02-08 22:36:44 +01:00
continue;
2021-12-15 16:30:22 +01:00
var stack = item.getItem();
2019-02-08 22:36:44 +01:00
if (stack.isEmpty() || stack.getItem() != Items.CLOCK)
continue;
2022-06-27 15:24:04 +02:00
var dayGoal = Mth.floor(frame.getRotation() / 8F * 24000F) + 18000;
2021-12-15 16:30:22 +01:00
var current = this.level.getDayTime();
var toMove = (24000 - current % 24000 + dayGoal) % 24000;
2019-02-08 22:36:44 +01:00
this.goalTime = current + toMove;
this.sendToClients();
2021-12-08 00:31:29 +01:00
if (stack.getCount() <= 1) {
item.kill();
} else {
2019-02-08 22:36:44 +01:00
stack.shrink(1);
item.setItem(stack);
}
return;
}
}
if (this.goalTime > 0) {
this.goalTime = 0;
this.sendToClients();
}
2021-12-08 00:31:29 +01:00
} else if (this.goalTime > 0 && this.level.random.nextFloat() >= 0.25F) {
2021-12-15 16:30:22 +01:00
var angle = Math.toRadians(this.level.getDayTime() * 5F % 360);
var x = this.worldPosition.getX() + 0.5 + Math.sin(angle) * 3F;
var z = this.worldPosition.getZ() + 0.5 + Math.cos(angle) * 3F;
var color = this.goalTime % 24000 > 12000 ? 0xe2e2e2 : 0xffe926;
2019-02-09 19:58:07 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-04 15:40:09 +01:00
x, this.worldPosition.getY() + 0.1F, z,
2019-02-09 19:58:07 +01:00
0F, 0.12F, 0F,
2021-12-08 00:31:29 +01:00
color, 1F + this.level.random.nextFloat() * 2F,
this.level.random.nextInt(100) + 100, 0, false, true);
2019-02-09 19:58:07 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-04 15:40:09 +01:00
x, this.worldPosition.getY() + 0.1F, z,
2019-02-09 19:58:07 +01:00
0F, 0F, 0F,
2021-12-08 00:31:29 +01:00
IAuraType.forLevel(this.level).getColor(), 1F + this.level.random.nextFloat(),
2019-02-09 19:58:07 +01:00
150, 0, false, true);
2019-02-08 22:36:44 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2019-02-08 22:36:44 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK)
2020-01-21 21:04:44 +01:00
compound.putLong("goal", this.goalTime);
2019-02-08 22:36:44 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2019-02-08 22:36:44 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK)
this.goalTime = compound.getLong("goal");
}
}