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;
|
|
|
|
import net.minecraft.item.Items;
|
2019-02-08 22:36:44 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.network.play.server.SUpdateTimePacket;
|
2019-02-08 22:36:44 +01:00
|
|
|
import net.minecraft.server.management.PlayerList;
|
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.ITickable;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.world.ServerWorld;
|
2019-02-08 22:36:44 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TileEntityTimeChanger extends TileEntityImpl implements ITickable {
|
|
|
|
|
|
|
|
private long goalTime;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
if (!this.world.isRemote) {
|
2019-10-20 22:30:49 +02:00
|
|
|
List<ItemFrameEntity> frames = Helper.getAttachedItemFrames(this.world, this.pos);
|
|
|
|
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) {
|
|
|
|
long current = this.world.getWorldTime();
|
|
|
|
long toAdd = Math.min(75, this.goalTime - current);
|
|
|
|
if (toAdd <= 0) {
|
|
|
|
this.goalTime = 0;
|
|
|
|
this.sendToClients();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.world.setWorldTime(current + toAdd);
|
|
|
|
|
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 35, this.pos);
|
|
|
|
IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, (int) toAdd * 20);
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
if (this.world instanceof ServerWorld) {
|
2019-02-08 22:36:44 +01:00
|
|
|
PlayerList list = this.world.getMinecraftServer().getPlayerList();
|
2019-10-20 22:30:49 +02:00
|
|
|
list.sendPacketToAllPlayersInDimension(new SUpdateTimePacket(
|
2019-02-08 22:36:44 +01:00
|
|
|
this.world.getTotalWorldTime(), this.world.getWorldTime(),
|
|
|
|
this.world.getGameRules().getBoolean("doDaylightCycle")), this.world.provider.getDimension());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.world.getTotalWorldTime() % 20 != 0)
|
|
|
|
return;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class,
|
|
|
|
new AxisAlignedBB(this.pos).grow(1), EntityPredicates.IS_ALIVE);
|
|
|
|
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;
|
|
|
|
long current = this.world.getWorldTime();
|
|
|
|
long toMove = (24000 - current % 24000 + dayGoal) % 24000;
|
|
|
|
this.goalTime = current + toMove;
|
|
|
|
this.sendToClients();
|
|
|
|
|
|
|
|
if (stack.getCount() <= 1)
|
|
|
|
item.setDead();
|
|
|
|
else {
|
|
|
|
stack.shrink(1);
|
|
|
|
item.setItem(stack);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.goalTime > 0) {
|
|
|
|
this.goalTime = 0;
|
|
|
|
this.sendToClients();
|
|
|
|
}
|
2019-02-09 19:58:07 +01:00
|
|
|
} else if (this.goalTime > 0 && this.world.rand.nextFloat() >= 0.25F) {
|
|
|
|
double angle = Math.toRadians(this.world.getTotalWorldTime() * 5F % 360);
|
|
|
|
double x = this.pos.getX() + 0.5 + Math.sin(angle) * 3F;
|
|
|
|
double z = this.pos.getZ() + 0.5 + Math.cos(angle) * 3F;
|
|
|
|
int color = this.goalTime % 24000 > 12000 ? 0xe2e2e2 : 0xffe926;
|
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
|
|
|
x, this.pos.getY() + 0.1F, z,
|
|
|
|
0F, 0.12F, 0F,
|
|
|
|
color, 1F + this.world.rand.nextFloat() * 2F,
|
|
|
|
this.world.rand.nextInt(100) + 100, 0, false, true);
|
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
|
|
|
x, this.pos.getY() + 0.1F, z,
|
|
|
|
0F, 0F, 0F,
|
|
|
|
IAuraType.forWorld(this.world).getColor(), 1F + this.world.rand.nextFloat(),
|
|
|
|
150, 0, false, true);
|
2019-02-08 22:36:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
public void writeNBT(CompoundNBT compound, SaveType type) {
|
2019-02-08 22:36:44 +01:00
|
|
|
super.writeNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK)
|
|
|
|
compound.setLong("goal", this.goalTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
public void readNBT(CompoundNBT compound, SaveType type) {
|
2019-02-08 22:36:44 +01:00
|
|
|
super.readNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK)
|
|
|
|
this.goalTime = compound.getLong("goal");
|
|
|
|
}
|
|
|
|
}
|