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

128 lines
5.4 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.item.ItemFrameEntity;
2019-02-08 22:36:44 +01:00
import net.minecraft.item.ItemStack;
2020-01-21 21:04:44 +01:00
import net.minecraft.item.Items;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2019-10-20 22:30:49 +02:00
import net.minecraft.network.play.server.SUpdateTimePacket;
2019-02-08 22:36:44 +01:00
import net.minecraft.server.management.PlayerList;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.ITickableBlockEntity;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.EntityPredicates;
2019-02-08 22:36:44 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.GameRules;
import net.minecraft.level.server.ServerLevel;
import net.minecraft.level.storage.IServerLevelInfo;
2019-02-08 22:36:44 +01:00
import java.util.List;
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-04 15:40:09 +01:00
public BlockEntityTimeChanger() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.TIME_CHANGER);
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) {
List<ItemFrameEntity> frames = Helper.getAttachedItemFrames(this.level, this.worldPosition);
2019-10-20 22:30:49 +02:00
for (ItemFrameEntity frame : frames) {
2019-02-08 22:36:44 +01:00
ItemStack frameStack = frame.getDisplayedItem();
if (frameStack.isEmpty() || frameStack.getItem() != ModItems.CLOCK_HAND)
continue;
if (this.goalTime > 0) {
2021-12-04 15:40:09 +01:00
long current = this.level.getDayTime();
2019-02-08 22:36:44 +01:00
long toAdd = Math.min(75, this.goalTime - current);
if (toAdd <= 0) {
this.goalTime = 0;
this.sendToClients();
return;
}
2021-12-04 15:40:09 +01:00
((IServerLevelInfo) this.level.getLevelInfo()).setDayTime(current + toAdd);
2019-02-08 22:36:44 +01:00
2021-12-04 15:40:09 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
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) {
PlayerList list = this.level.getServer().getPlayerList();
2020-09-22 03:17:02 +02:00
list.sendPacketToAllPlayers(new SUpdateTimePacket(
2021-12-04 15:40:09 +01:00
this.level.getGameTime(), this.level.getDayTime(),
this.level.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE)));
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-04 15:40:09 +01:00
List<ItemEntity> items = this.level.getEntitiesWithinAABB(ItemEntity.class,
new AxisAlignedBB(this.worldPosition).grow(1), EntityPredicates.IS_ALIVE);
2019-10-20 22:30:49 +02:00
for (ItemEntity item : items) {
2019-02-08 22:36:44 +01:00
if (item.cannotPickup())
continue;
ItemStack stack = item.getItem();
if (stack.isEmpty() || stack.getItem() != Items.CLOCK)
continue;
int dayGoal = MathHelper.floor((frame.getRotation() / 8F) * 24000F) + 18000;
2021-12-04 15:40:09 +01:00
long current = this.level.getDayTime();
2019-02-08 22:36:44 +01:00
long toMove = (24000 - current % 24000 + dayGoal) % 24000;
this.goalTime = current + toMove;
this.sendToClients();
if (stack.getCount() <= 1)
2020-01-21 21:04:44 +01:00
item.remove();
2019-02-08 22:36:44 +01:00
else {
stack.shrink(1);
item.setItem(stack);
}
return;
}
}
if (this.goalTime > 0) {
this.goalTime = 0;
this.sendToClients();
}
2021-12-04 15:40:09 +01:00
} else if (this.goalTime > 0 && this.level.rand.nextFloat() >= 0.25F) {
double angle = Math.toRadians(this.level.getDayTime() * 5F % 360);
double x = this.worldPosition.getX() + 0.5 + Math.sin(angle) * 3F;
double z = this.worldPosition.getZ() + 0.5 + Math.cos(angle) * 3F;
2019-02-09 19:58:07 +01:00
int color = this.goalTime % 24000 > 12000 ? 0xe2e2e2 : 0xffe926;
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-04 15:40:09 +01:00
color, 1F + this.level.rand.nextFloat() * 2F,
this.level.rand.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-04 15:40:09 +01:00
IAuraType.forLevel(this.level).getColor(), 1F + this.level.rand.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");
}
}