Skip to content

Commit

Permalink
Added client-server sync for crystal marker and lectern.
Browse files Browse the repository at this point in the history
Expect performance drop.
  • Loading branch information
EdwinMindcraft committed Mar 22, 2018
1 parent 1fcee6c commit 85dbc4f
Show file tree
Hide file tree
Showing 5 changed files with 717 additions and 702 deletions.
224 changes: 114 additions & 110 deletions src/main/java/am2/client/blocks/render/TileCrystalMarkerRenderer.java
Original file line number Diff line number Diff line change
@@ -1,110 +1,114 @@
package am2.client.blocks.render;

import org.lwjgl.opengl.GL11;

import am2.common.blocks.BlockCrystalMarker;
import am2.common.blocks.tileentity.TileEntityCrystalMarker;
import am2.common.defs.BlockDefs;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.model.TRSRTransformation;

public class TileCrystalMarkerRenderer extends TileEntitySpecialRenderer<TileEntityCrystalMarker>{
private IModel model;
private IBakedModel bakedModel;

private IBakedModel getBakedModel() {
try {
model = ModelLoaderRegistry.getModel(new ResourceLocation("arsmagica2", "block/crystal_marker.obj"));
} catch (Exception e) {
throw new RuntimeException(e);
}
bakedModel = model.bake(TRSRTransformation.identity(), DefaultVertexFormats.ITEM, location -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(location.toString()));
return bakedModel;
}


public TileCrystalMarkerRenderer(){
}

public void renderTileEntityAt(TileEntityCrystalMarker tileentity, double x, double y, double z, float partialTicks, int destroyStage){
EnumFacing facing = EnumFacing.UP;

if (tileentity.getWorld() != null){
facing = tileentity.getFacing();
}

GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_LIGHTING_BIT);
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
GL11.glDisable(GL11.GL_CULL_FACE);
RenderHelper.disableStandardItemLighting();

if (tileentity.getPos() != BlockPos.ORIGIN){
switch (facing){
case UP: //Bottom, Inventory is above
GL11.glTranslated(x + 0.5, y + 1.0 + tileentity.GetConnectedBoundingBox().minY, z + 0.5);
GL11.glRotated(90, 1, 0, 0);
break;
case DOWN: //Top, Inventory is below
GL11.glTranslated(x + 0.5, y - (1.0 - tileentity.GetConnectedBoundingBox().maxY), z + 0.5);
GL11.glRotated(270, 1, 0, 0);
break;
case SOUTH: //North, Inventory is to the south
GL11.glTranslated(x + 0.5, y + 0.5, z + 1.0 + (1.0 - tileentity.GetConnectedBoundingBox().maxZ));
GL11.glRotated(180, 0, 1, 0);
break;
case NORTH: //South, Inventory is to the north
GL11.glTranslated(x + 0.5, y + 0.5, z - tileentity.GetConnectedBoundingBox().minZ);
break;
case EAST: //West, Inventory is to the east
GL11.glTranslated(x + 1 + tileentity.GetConnectedBoundingBox().minX, y + 0.5, z + 0.5);
GL11.glRotated(270, 0, 1, 0);
break;
case WEST: //East, Inventory is to the west
GL11.glTranslated(x - (1.0 - tileentity.GetConnectedBoundingBox().maxX), y + 0.5, z + 0.5);
GL11.glRotated(90, 0, 1, 0);
break;
}

GL11.glScalef(0.5f, 0.5f, 0.5f);
}else{
GL11.glTranslated(0.5, 0.5, 0.5);
GL11.glScalef(1.4f, 1.4f, 1.4f);
GL11.glRotated(180, 0, 1, 0);
}

int blockType = 0;

if (tileentity.getWorld() != null && destroyStage != -10){
blockType = tileentity.getWorld().getBlockState(tileentity.getPos()).getValue(BlockCrystalMarker.TYPE);
}else{
blockType = (int)partialTicks;
}

GlStateManager.pushMatrix();
GlStateManager.translate(-tileentity.getPos().getX(), -tileentity.getPos().getY(), -tileentity.getPos().getZ());
Tessellator tesselator = Tessellator.getInstance();
tesselator.getBuffer().begin(7, DefaultVertexFormats.BLOCK);
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModel(getWorld(), getBakedModel(), BlockDefs.crystalMarker.getDefaultState().withProperty(BlockCrystalMarker.TYPE, blockType), tileentity.getPos(), tesselator.getBuffer(), false);
tesselator.draw();
GlStateManager.popMatrix();
RenderHelper.enableStandardItemLighting();
GL11.glPopAttrib();
GL11.glEnable(GL11.GL_CULL_FACE);
GlStateManager.enableBlend();
GL11.glPopMatrix();
}

}
package am2.client.blocks.render;

import org.lwjgl.opengl.GL11;

import am2.common.blocks.BlockCrystalMarker;
import am2.common.blocks.tileentity.TileEntityCrystalMarker;
import am2.common.defs.BlockDefs;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.model.TRSRTransformation;

public class TileCrystalMarkerRenderer extends TileEntitySpecialRenderer<TileEntityCrystalMarker>{
private IModel model;
private IBakedModel bakedModel;

private IBakedModel getBakedModel() {
if (bakedModel == null) {
try {
model = ModelLoaderRegistry.getModel(new ResourceLocation("arsmagica2", "block/crystal_marker.obj"));
} catch (Exception e) {
throw new RuntimeException(e);
}
bakedModel = model.bake(TRSRTransformation.identity(), DefaultVertexFormats.ITEM, location -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(location.toString()));
}
return bakedModel;
}


public TileCrystalMarkerRenderer(){
}

@Override
public void renderTileEntityAt(TileEntityCrystalMarker tileentity, double x, double y, double z, float partialTicks, int destroyStage){
EnumFacing facing = EnumFacing.UP;

if (tileentity.getWorld() != null){
facing = tileentity.getFacing();
//facing = tileentity.getWorld().getBlockState(tileentity.getPos()).getValue(BlockCrystalMarker.FACING);
}

GlStateManager.pushMatrix();
GlStateManager.pushAttrib();//(GL11.GL_LIGHTING_BIT);
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
GlStateManager.disableCull();
RenderHelper.disableStandardItemLighting();

if (tileentity.getPos() != BlockPos.ORIGIN){
switch (facing){
case UP: //Bottom, Inventory is above
GL11.glTranslated(x + 0.5, y + 1.0 + tileentity.GetConnectedBoundingBox().minY, z + 0.5);
GL11.glRotated(90, 1, 0, 0);
break;
case DOWN: //Top, Inventory is below
GL11.glTranslated(x + 0.5, y - (1.0 - tileentity.GetConnectedBoundingBox().maxY), z + 0.5);
GL11.glRotated(270, 1, 0, 0);
break;
case SOUTH: //North, Inventory is to the south
GL11.glTranslated(x + 0.5, y + 0.5, z + 1.0 + (1.0 - tileentity.GetConnectedBoundingBox().maxZ));
GL11.glRotated(180, 0, 1, 0);
break;
case NORTH: //South, Inventory is to the north
GL11.glTranslated(x + 0.5, y + 0.5, z - tileentity.GetConnectedBoundingBox().minZ);
break;
case EAST: //West, Inventory is to the east
GL11.glTranslated(x + 1 + tileentity.GetConnectedBoundingBox().minX, y + 0.5, z + 0.5);
GL11.glRotated(270, 0, 1, 0);
break;
case WEST: //East, Inventory is to the west
GL11.glTranslated(x - (1.0 - tileentity.GetConnectedBoundingBox().maxX), y + 0.5, z + 0.5);
GL11.glRotated(90, 0, 1, 0);
break;
}

GL11.glScalef(0.5f, 0.5f, 0.5f);
}else{
GL11.glTranslated(0.5, 0.5, 0.5);
GL11.glScalef(1.4f, 1.4f, 1.4f);
GL11.glRotated(180, 0, 1, 0);
}

int blockType = 0;

if (tileentity.getWorld() != null && destroyStage != -10){
blockType = tileentity.getWorld().getBlockState(tileentity.getPos()).getValue(BlockCrystalMarker.TYPE);
}else{
blockType = (int)partialTicks;
}

GlStateManager.pushMatrix();
GlStateManager.translate(-tileentity.getPos().getX(), -tileentity.getPos().getY(), -tileentity.getPos().getZ());
Tessellator tesselator = Tessellator.getInstance();
tesselator.getBuffer().begin(7, DefaultVertexFormats.BLOCK);
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModel(getWorld(), getBakedModel(), BlockDefs.crystalMarker.getDefaultState().withProperty(BlockCrystalMarker.TYPE, blockType), tileentity.getPos(), tesselator.getBuffer(), false);
tesselator.draw();
GlStateManager.popMatrix();
RenderHelper.enableStandardItemLighting();
GlStateManager.popAttrib();
GlStateManager.enableCull();
GlStateManager.enableBlend();
GlStateManager.popMatrix();
}

}
54 changes: 27 additions & 27 deletions src/main/java/am2/common/blocks/tileentity/TileEntityAMPower.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package am2.common.blocks.tileentity;

import java.util.List;

import am2.api.power.IPowerNode;
import am2.common.power.PowerNodeRegistry;
import am2.common.power.PowerTypes;
Expand All @@ -10,99 +8,101 @@
import net.minecraft.util.ITickable;
import net.minecraft.world.World;

public abstract class TileEntityAMPower extends TileEntity implements IPowerNode<TileEntityAMPower>, ITickable{
import java.util.List;

public abstract class TileEntityAMPower extends TileEntity implements IPowerNode<TileEntityAMPower>, ITickable {
protected int capacity;
protected boolean canRequestPower = true;
private int tickCounter;

private static final int REQUEST_INTERVAL = 20;

public TileEntityAMPower(int capacity){
public TileEntityAMPower(int capacity) {
this.capacity = capacity;
}

protected void setNoPowerRequests(){
canRequestPower = false;
protected void setNoPowerRequests() {
this.canRequestPower = false;
}

protected void setPowerRequests(){
canRequestPower = true;
protected void setPowerRequests() {
this.canRequestPower = true;
}

/***
* Whether or not the tile entity is *capable* of providing power.
*/
@Override
public boolean canProvidePower(PowerTypes type){
public boolean canProvidePower(PowerTypes type) {
return false;
}

@Override
public void invalidate(){
public void invalidate() {
PowerNodeRegistry.For(this.worldObj).removePowerNode(this);
super.invalidate();
}

@Override
public void update(){
if (!worldObj.isRemote && this.canRequestPower() && tickCounter++ >= getRequestInterval()){
tickCounter = 0;
public void update() {
if (!this.worldObj.isRemote && this.canRequestPower() && this.tickCounter++ >= this.getRequestInterval()) {
this.tickCounter = 0;
List<PowerTypes> powerTypes = this.getValidPowerTypes();
for (PowerTypes type : powerTypes){
float amtObtained = PowerNodeRegistry.For(worldObj).requestPower(this, type, this.getChargeRate());
for (PowerTypes type : powerTypes) {
float amtObtained = PowerNodeRegistry.For(this.worldObj).requestPower(this, type, this.getChargeRate());
if (amtObtained > 0)
PowerNodeRegistry.For(worldObj).insertPower(this, type, amtObtained);
PowerNodeRegistry.For(this.worldObj).insertPower(this, type, amtObtained);
}
}
//worldObj.markAndNotifyBlock(pos, worldObj.getChunkFromBlockCoords(pos), worldObj.getBlockState(pos), worldObj.getBlockState(pos), 3);
}

public int getRequestInterval(){
public int getRequestInterval() {
return REQUEST_INTERVAL;
}

@Override
public float particleOffset(int axis){
public float particleOffset(int axis) {
return 0.5f;
}

@Override
public void readFromNBT(NBTTagCompound nbttagcompound){
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
}

@Override
public void setWorldObj(World par1World){
public void setWorldObj(World par1World) {
super.setWorldObj(par1World);
PowerNodeRegistry.For(this.worldObj).registerPowerNode(this);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound){
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
return super.writeToNBT(nbttagcompound);
}

@Override
public float getCapacity(){
public float getCapacity() {
return this.capacity;
}

public void setPower(PowerTypes type, float amount){
public void setPower(PowerTypes type, float amount) {
PowerNodeRegistry.For(this.worldObj).setPower(this, type, amount);
}

@Override
public List<PowerTypes> getValidPowerTypes(){
public List<PowerTypes> getValidPowerTypes() {
return PowerTypes.all();
}

@Override
public boolean canRequestPower(){
return canRequestPower;
public boolean canRequestPower() {
return this.canRequestPower;
}

@Override
public boolean isSource(){
public boolean isSource() {
return false;
}
}
Loading

0 comments on commit 85dbc4f

Please sign in to comment.