Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stormbot28.mod.magicmod.blocks.qol;
- import com.stormbot28.mod.magicmod.Registering;
- import net.minecraft.block.Blocks;
- import net.minecraft.block.ShapeContext;
- import net.minecraft.block.entity.BlockEntity;
- import net.minecraft.item.ItemPlacementContext;
- import net.minecraft.nbt.CompoundTag;
- import net.minecraft.particle.ParticleTypes;
- import net.minecraft.util.Tickable;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.chunk.WorldChunk;
- /**
- * Move contents to add/remove error
- *In order of importance
- * 1. torches placing and not updating player/client renderer
- * 2. save/load data
- *
- *
- *
- *
- */
- //###@Plans make it eat coal/!!glowstone!!/torches to run
- public class IlluminatorBlockEntity extends BlockEntity implements Tickable {
- private static final int ILLUMINATOR_RADIUS = 3;
- private static final int ILLUMINATOR_DIAMETER = (ILLUMINATOR_RADIUS * 2) + 1;
- private int waitTickCounter = 0;
- private boolean initialized = false;
- private WorldChunk startingWorldChunk; // should be final :(, but world isnt set
- private int xChunkInit; // should be final :(, but world isnt set
- private int zChunkInit; // should be final :(, but world isnt set
- private int xChunkIter;
- private int zChunkIter;
- private int xInit;
- private int zInit;
- private int xIter;
- private int zIter;
- public IlluminatorBlockEntity() {
- super(Registering.ILLUMINATOR_BLOCK_ENTITY_TYPE);
- }
- private void initialize() {
- if(initialized) return;
- this.startingWorldChunk = this.world.getWorldChunk(this.pos);
- this.xChunkInit = startingWorldChunk.getPos().x - ILLUMINATOR_RADIUS;
- this.zChunkInit = startingWorldChunk.getPos().z - ILLUMINATOR_RADIUS;
- System.out.println("X_CHUNK_INIT: "+xChunkInit);
- System.out.println("X_STARTING_CHUNK_POS: "+startingWorldChunk.getPos().x);
- System.out.println("ILLUMINATOR_RADIUS: "+ILLUMINATOR_RADIUS);
- resetCounters();
- System.out.println("X_INIT: "+xInit);
- initialized = true;
- }
- private void resetCounters() {
- BlockPos initialBlockPos = world.getChunk(xChunkInit, zChunkInit).getPos().getStartPos();
- xInit = initialBlockPos.getX();
- zInit = initialBlockPos.getZ();
- xIter = 0;
- zIter = 0;
- xChunkIter = 0;
- zChunkIter = 0;
- System.out.println("counters reset");
- }
- @Override
- public void tick() {
- initialize();
- if(waitTickCounter > 0) { // test if cooldown ticking
- System.out.println("waiting: " + waitTickCounter);
- if(++waitTickCounter < 20*60*15) //20 * 60 * 15 (15 minutes between updates)
- return;
- else // once cooldown ends, relight
- waitTickCounter = 0;
- }
- // light up current column
- lightUpWorldColumn(xInit + xIter, zInit + zIter);
- //increment iterators accordingly
- System.out.println("==============================================");
- System.out.println("xChunkIter: " + xChunkIter);
- System.out.println("zChunkIter: " + zChunkIter);
- System.out.println("xChunkInit: " + xChunkInit);
- System.out.println("zChunkInit: " + zChunkInit);
- if(++xIter > 15) { // move through Z row of chunk
- xIter = 0;
- if(++zIter > 15) { // move through next Z row of chunk
- System.out.println("---resetZ---OLD: " + zIter +" + " + zInit + " = " + (zIter+zInit));
- zIter = 0;
- if(++xChunkIter >= ILLUMINATOR_DIAMETER) { // move through Z row of selected chunks
- System.out.println("move z and x chunks");
- xChunkIter = 0;
- if(++zChunkIter >= ILLUMINATOR_DIAMETER) { // next Z row of selected chunks
- // We've reached the end, reset and await cooldown
- resetCounters();
- waitTickCounter = 1;
- }
- }
- // whether we've only reached the end of the x chunk, or the far end, we need to reset the init coords
- // remember, xChunkIter changes whether or not if statement is true
- realignInitOnChunk();
- System.out.println("---resetZ---NEW: " + zIter +" + " + zInit + " = " + (zIter+zInit));
- }
- }
- }
- private void realignInitOnChunk() {
- BlockPos startingPos = world.getChunk(xChunkInit + xChunkIter, zChunkInit + zChunkIter).getPos().getStartPos();
- xInit = startingPos.getX();
- zInit = startingPos.getZ();
- System.out.println("realign init on chunk");
- }
- private void lightUpWorldColumn(int x, int z) {
- BlockPos currentBlockPos = new BlockPos(x, 1, z);
- WorldChunk currentWorldChunk = world.getWorldChunk(currentBlockPos);
- currentWorldChunk.loadToWorld();
- boolean isDirty = false;
- while(currentBlockPos.getY() < world.getHeight()) {
- currentBlockPos = currentBlockPos.up();
- //System.out.println("block: " + world.getBlockState(currentBlockPos).getBlock().getTranslationKey() + "|||isAir: "+world.getBlockState(currentBlockPos).isAir());
- if(!world.getBlockState(currentBlockPos).isAir() ){
- //System.out.println("not air");
- if(world.getBlockState(currentBlockPos.up()).isAir() ) {
- currentBlockPos = currentBlockPos.up();
- world.addParticle(ParticleTypes.SOUL, true, currentBlockPos.getX(), currentBlockPos.getY() + 1, currentBlockPos.getZ(), 0, 0, 0);
- //System.out.println("under air with light: "+world.getLightLevel(currentBlockPos));
- if (world.getLightLevel(currentBlockPos) <= 7) {
- //System.out.println("bad light level at y="+currentBlockPos.getY());
- if (isDirty = world.canPlace(Blocks.TORCH.getDefaultState(), currentBlockPos, ShapeContext.absent())) {
- /*BlockState bs =*/
- world.getWorldChunk(currentBlockPos).setBlockState(currentBlockPos, Blocks.TORCH.getDefaultState(), false);
- //currentWorldChunk.markBlockForPostProcessing(currentBlockPos);
- world.addParticle(ParticleTypes.CAMPFIRE_COSY_SMOKE, true, currentBlockPos.getX(), currentBlockPos.getY() + 1, currentBlockPos.getZ(), 0, 0, 0);
- world.getLightingProvider().checkBlock(currentBlockPos);
- System.out.println("torch placed @:" + x + ", " + z);
- }
- }
- }
- }
- }
- //System.out.println("light column @:" +x+", "+z);
- if(isDirty) {
- // real janky force re-render
- //for(PlayerEntity p : world.getPlayers())
- //world.getServer().getWorld(world.getRegistryKey()).getChunkManager().addTicket(ChunkTicketType.POST_TELEPORT, currentWorldChunk.getPos(), 0, p.getEntityId());
- //for(PlayerEntity p : world.getPlayers())
- // if(p.getBlockPos())
- // currentWorldChunk.markBlockForPostProcessing();
- currentWorldChunk.markDirty(); // tells world it needs to save, do this after rendering update
- }
- }
- /*
- private int waitTickCounter = 0;
- private boolean initialized = false;
- private WorldChunk startingWorldChunk; // should be final :(, but world isnt set
- private int xChunkInit; // should be final :(, but world isnt set
- private int zChunkInit; // should be final :(, but world isnt set
- private int xChunkIter;
- private int zChunkIter;
- private int xInit;
- private int zInit;
- private int xIter;
- private int zIter;
- */
- // Serialize the BlockEntity
- public CompoundTag toTag(CompoundTag tag) {
- super.toTag(tag);
- // Save the current value of the number to the tag
- tag.putInt("waitTickCounter", waitTickCounter);
- tag.putInt("xChunkIter", xChunkIter);
- tag.putInt("zChunkIter", zChunkIter);
- tag.putInt("xInit", xInit);
- tag.putInt("zInit", zInit);
- tag.putInt("xIter", xIter);
- tag.putInt("zIter", zIter);
- return tag;
- }
- // Deserialize the BlockEntity
- public void fromTag(CompoundTag tag) {
- this.pos = new BlockPos(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
- initialize();
- waitTickCounter= tag.getInt("waitTickCounter");
- xChunkIter = tag.getInt("xChunkIter");
- zChunkIter = tag.getInt("zChunkIter");
- xInit = tag.getInt("xInit");
- zInit = tag.getInt("zInit");
- xIter = tag.getInt("xIter");
- zIter = tag.getInt("zIter");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement