Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.wesserboy.inWorldStorage.client.events;
import com.wesserboy.inWorldStorage.blocks.BlockVisionMover;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ClientEventHandler {
double orgX;
double orgY;
double orgZ;
double orgPrevX;
double orgPrevY;
double orgPrevZ;
boolean wasChanged;
boolean wasOnBlockPrevTick;
@SubscribeEvent
public void onRenderTick(RenderTickEvent event){
Minecraft mc = Minecraft.getMinecraft();
if(event.phase == TickEvent.Phase.START){
if(mc.thePlayer != null && mc.theWorld != null){
IBlockState blockBelow = mc.theWorld.getBlockState((new BlockPos(mc.thePlayer)).add(0, -1, 0));
if(blockBelow.getBlock() instanceof BlockVisionMover){
wasOnBlockPrevTick = true;
//Hardcoded position for testing purposes, eventually fetched using packets.
BlockPos newPos = new BlockPos(mc.thePlayer.posX + 200, mc.thePlayer.posY + 3, mc.thePlayer.posZ);
Entity e = mc.getRenderViewEntity();
orgX = e.posX;
orgY = e.posY;
orgZ = e.posZ;
orgPrevX = e.prevPosX;
orgPrevY = e.prevPosY;
orgPrevZ = e.prevPosZ;
e.lastTickPosX = e.prevPosX = e.posX = newPos.getX() + 0.5D;
e.lastTickPosY = e.prevPosY = e.posY = newPos.getY() + 0.5D;
e.lastTickPosZ = e.prevPosZ = e.posZ = newPos.getZ() + 0.5D;
wasChanged = true;
if(mc.theWorld.getChunkFromBlockCoords(newPos).isEmpty()){
//System.out.println("The chunk is not loaded!");
mc.theWorld.getChunkProvider().loadChunk(newPos.getX() / 16, newPos.getZ() / 16);
}
}else{
if(wasOnBlockPrevTick){
for(int i = -mc.gameSettings.renderDistanceChunks; i <= mc.gameSettings.renderDistanceChunks; i++){
for(int j = -mc.gameSettings.renderDistanceChunks; j<= mc.gameSettings.renderDistanceChunks; j++){
BlockPos blockInChunk = mc.thePlayer.getPosition();
blockInChunk = blockInChunk.add(16 * i, 0, 16 * j);
System.out.println("Chunk at: " + blockInChunk.getX()/16 + ", " + blockInChunk.getZ()/16 + " marked dirty");
mc.theWorld.markChunkDirty(blockInChunk, null);
}
}
wasOnBlockPrevTick = false;
}
}
}
}else{
if(wasChanged){
Entity e = mc.getRenderViewEntity();
e.prevPosX = orgPrevX;
e.prevPosY = orgPrevY;
e.prevPosZ = orgPrevZ;
e.posX = orgX;
e.posY = orgY;
e.posZ = orgZ;
wasChanged = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.