From 0350bd36fce5f74c36f56e79341750c304464db9 Mon Sep 17 00:00:00 2001 From: MrJake222 Date: Mon, 8 Feb 2021 18:04:47 +0100 Subject: [PATCH] Added snow under DHD and automatic snow camo in gate blocks Fixed #257 --- src/main/java/mrjake/aunis/AunisProps.java | 6 +++ .../java/mrjake/aunis/block/DHDBlock.java | 27 ++++++++++- .../stargate/StargateAbstractMemberBlock.java | 15 ++++-- .../stargate/StargateClassicMemberBlock.java | 35 +++++++++----- .../mrjake/aunis/renderer/BlockRenderer.java | 7 ++- .../mrjake/aunis/renderer/DHDRenderer.java | 11 ++++- .../stargate/StargateAbstractRenderer.java | 11 +---- .../merging/StargateClassicMergeHelper.java | 11 ++--- .../stargate/StargateClassicMemberTile.java | 46 +++++++++++++++---- 9 files changed, 124 insertions(+), 45 deletions(-) diff --git a/src/main/java/mrjake/aunis/AunisProps.java b/src/main/java/mrjake/aunis/AunisProps.java index 3ee4fb10..3023882e 100644 --- a/src/main/java/mrjake/aunis/AunisProps.java +++ b/src/main/java/mrjake/aunis/AunisProps.java @@ -4,6 +4,7 @@ import mrjake.aunis.block.BeamerBlock; import mrjake.aunis.block.CapacitorBlock; +import mrjake.aunis.block.DHDBlock; import mrjake.aunis.block.InvisibleBlock; import mrjake.aunis.block.stargate.StargateMilkyWayMemberBlock; import mrjake.aunis.block.stargate.StargateOrlinMemberBlock; @@ -76,4 +77,9 @@ public class AunisProps { * Used by {@link BeamerBlock} to display textures. */ public static final PropertyBeamerMode BEAMER_MODE = PropertyBeamerMode.create("mode"); + + /** + * Used by {@link DHDBlock} to determine whether it's snow layer on the bottom should be rendered. + */ + public static final PropertyBool SNOWY = PropertyBool.create("snowy"); } diff --git a/src/main/java/mrjake/aunis/block/DHDBlock.java b/src/main/java/mrjake/aunis/block/DHDBlock.java index 8332fe36..43c77465 100644 --- a/src/main/java/mrjake/aunis/block/DHDBlock.java +++ b/src/main/java/mrjake/aunis/block/DHDBlock.java @@ -17,8 +17,10 @@ import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; +import net.minecraft.block.state.pattern.BlockMatcher; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumBlockRenderType; @@ -46,7 +48,8 @@ public DHDBlock() { setCreativeTab(Aunis.aunisCreativeTab); setDefaultState(blockState.getBaseState() - .withProperty(AunisProps.ROTATION_HORIZONTAL, 0)); + .withProperty(AunisProps.ROTATION_HORIZONTAL, 0) + .withProperty(AunisProps.SNOWY, false)); setLightOpacity(0); @@ -58,7 +61,7 @@ public DHDBlock() { // ------------------------------------------------------------------------ @Override protected BlockStateContainer createBlockState() { - return new BlockStateContainer(this, AunisProps.ROTATION_HORIZONTAL); + return new BlockStateContainer(this, AunisProps.ROTATION_HORIZONTAL, AunisProps.SNOWY); } @Override @@ -73,6 +76,26 @@ public IBlockState getStateFromMeta(int meta) { .withProperty(AunisProps.ROTATION_HORIZONTAL, meta); } + public final static BlockMatcher SNOW_MATCHER = BlockMatcher.forBlock(Blocks.SNOW_LAYER); + + @Override + public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) { + return state.withProperty(AunisProps.SNOWY, isSnowAroundBlock(world, pos)); + } + + public static boolean isSnowAroundBlock(IBlockAccess world, BlockPos inPos) { + + // Check if 4 adjacent blocks are snow layers + for (EnumFacing facing : EnumFacing.HORIZONTALS) { + BlockPos pos = inPos.offset(facing); + if (!SNOW_MATCHER.apply(world.getBlockState(pos))) { + return false; + } + } + + return true; + } + // ------------------------------------------------------------------------ @Override public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { diff --git a/src/main/java/mrjake/aunis/block/stargate/StargateAbstractMemberBlock.java b/src/main/java/mrjake/aunis/block/stargate/StargateAbstractMemberBlock.java index 81d93fd3..2630c369 100644 --- a/src/main/java/mrjake/aunis/block/stargate/StargateAbstractMemberBlock.java +++ b/src/main/java/mrjake/aunis/block/stargate/StargateAbstractMemberBlock.java @@ -7,6 +7,7 @@ import mrjake.aunis.stargate.merging.StargateAbstractMergeHelper; import mrjake.aunis.tileentity.stargate.StargateAbstractBaseTile; import mrjake.aunis.tileentity.stargate.StargateAbstractMemberTile; +import mrjake.aunis.tileentity.stargate.StargateClassicMemberTile; import net.minecraft.block.Block; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; @@ -46,7 +47,7 @@ public StargateAbstractMemberBlock(String blockName) { public void breakBlock(World world, BlockPos pos, IBlockState state) { StargateAbstractMemberTile memberTile = (StargateAbstractMemberTile) world.getTileEntity(pos); StargateAbstractBaseTile gateTile = memberTile.getBaseTile(world); - + if (gateTile != null) { gateTile.updateMergeState(false, world.getBlockState(gateTile.getPos()).getValue(AunisProps.FACING_HORIZONTAL)); } @@ -104,7 +105,15 @@ public boolean isFullBlock(IBlockState state) { } @Override - public BlockFaceShape getBlockFaceShape(IBlockAccess worldIn, IBlockState state, BlockPos pos, EnumFacing face) { - return BlockFaceShape.UNDEFINED; + public BlockFaceShape getBlockFaceShape(IBlockAccess world, IBlockState state, BlockPos pos, EnumFacing facing) { + if (state.getValue(AunisProps.RENDER_BLOCK)) { + // Rendering some block + StargateClassicMemberTile memberTile = (StargateClassicMemberTile) world.getTileEntity(pos); + if (memberTile != null && memberTile.getCamoState() != null) { + return memberTile.getCamoState().getBlockFaceShape(world, pos, facing); + } + } + + return BlockFaceShape.UNDEFINED; } } diff --git a/src/main/java/mrjake/aunis/block/stargate/StargateClassicMemberBlock.java b/src/main/java/mrjake/aunis/block/stargate/StargateClassicMemberBlock.java index 696c4591..33dfafba 100644 --- a/src/main/java/mrjake/aunis/block/stargate/StargateClassicMemberBlock.java +++ b/src/main/java/mrjake/aunis/block/stargate/StargateClassicMemberBlock.java @@ -3,12 +3,10 @@ import mrjake.aunis.Aunis; import mrjake.aunis.AunisProps; import mrjake.aunis.block.AunisBlocks; +import mrjake.aunis.block.DHDBlock; import mrjake.aunis.gui.GuiIdEnum; -import mrjake.aunis.packet.AunisPacketHandler; -import mrjake.aunis.packet.StateUpdatePacketToClient; import mrjake.aunis.stargate.CamoPropertiesHelper; import mrjake.aunis.stargate.EnumMemberVariant; -import mrjake.aunis.state.StateTypeEnum; import mrjake.aunis.tileentity.stargate.StargateAbstractBaseTile; import mrjake.aunis.tileentity.stargate.StargateClassicMemberTile; import net.minecraft.block.Block; @@ -41,7 +39,6 @@ import net.minecraftforge.common.property.ExtendedBlockState; import net.minecraftforge.common.property.IExtendedBlockState; import net.minecraftforge.common.property.IUnlistedProperty; -import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; public abstract class StargateClassicMemberBlock extends StargateAbstractMemberBlock { @@ -127,10 +124,10 @@ public IBlockState getExtendedState(IBlockState state, IBlockAccess world, Block StargateClassicMemberTile memberTile = (StargateClassicMemberTile) world.getTileEntity(pos); if (memberTile != null) { - IBlockState doubleSlabState = memberTile.getCamoState(); - - if (doubleSlabState != null) { - return ((IExtendedBlockState) state).withProperty(AunisProps.CAMO_BLOCKSTATE, doubleSlabState); + IBlockState camoBlockState = memberTile.getCamoState(); + + if (camoBlockState != null) { + return ((IExtendedBlockState) state).withProperty(AunisProps.CAMO_BLOCKSTATE, camoBlockState); } } @@ -161,6 +158,23 @@ public void registerCustomModel(IRegistry re } } + @Override + public void neighborChanged(IBlockState state, World world, BlockPos pos, Block blockIn, BlockPos fromPos) { + StargateClassicMemberTile memberTile = (StargateClassicMemberTile) world.getTileEntity(pos); + + if (!world.isRemote && memberTile != null) { + // Server and tile entity exists + + if (memberTile.isMerged() && memberTile.getCamoState() == null || DHDBlock.SNOW_MATCHER.apply(memberTile.getCamoState())) { + // Merged and camo is empty or it's snow + boolean snowAround = DHDBlock.isSnowAroundBlock(world, pos); + + // Set camo to snow or null + memberTile.setCamoState(snowAround ? Blocks.SNOW_LAYER.getDefaultState() : null); + world.setBlockState(pos, state.withProperty(AunisProps.RENDER_BLOCK, snowAround)); + } + } + } // ------------------------------------------------------------------------ @@ -299,14 +313,11 @@ else if (blockSlab == Blocks.PURPUR_SLAB) else { world.setBlockState(pos, state.withProperty(AunisProps.RENDER_BLOCK, false), 0); } - - TargetPoint point = new TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 512); - AunisPacketHandler.INSTANCE.sendToAllTracking(new StateUpdatePacketToClient(pos, StateTypeEnum.CAMO_STATE, memberTile.getState(StateTypeEnum.CAMO_STATE)), point); return true; } - else { + else { return heldItem != Item.getItemFromBlock(AunisBlocks.STARGATE_MILKY_WAY_MEMBER_BLOCK) && heldItem != Item.getItemFromBlock(AunisBlocks.STARGATE_MILKY_WAY_BASE_BLOCK) && heldItem != Item.getItemFromBlock(AunisBlocks.STARGATE_UNIVERSE_BASE_BLOCK) && diff --git a/src/main/java/mrjake/aunis/renderer/BlockRenderer.java b/src/main/java/mrjake/aunis/renderer/BlockRenderer.java index 009241d9..51a49eeb 100644 --- a/src/main/java/mrjake/aunis/renderer/BlockRenderer.java +++ b/src/main/java/mrjake/aunis/renderer/BlockRenderer.java @@ -8,6 +8,7 @@ import net.minecraft.client.renderer.GlStateManager; 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.vertex.DefaultVertexFormats; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -19,12 +20,16 @@ public static void render(World world, BlockPos pos, IBlockState state) { IBakedModel model = blockRendererDispatcher.getModelForState(state); GlStateManager.disableLighting(); - + Minecraft.getMinecraft().entityRenderer.disableLightmap(); + Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE); + Tessellator tessellator = Tessellator.getInstance(); tessellator.getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK); blockRendererDispatcher.getBlockModelRenderer().renderModelFlat(world, model, state, pos, tessellator.getBuffer(), false, 0); tessellator.draw(); GlStateManager.enableLighting(); + Minecraft.getMinecraft().entityRenderer.enableLightmap(); + } } diff --git a/src/main/java/mrjake/aunis/renderer/DHDRenderer.java b/src/main/java/mrjake/aunis/renderer/DHDRenderer.java index 56397800..220f2a93 100644 --- a/src/main/java/mrjake/aunis/renderer/DHDRenderer.java +++ b/src/main/java/mrjake/aunis/renderer/DHDRenderer.java @@ -1,11 +1,14 @@ package mrjake.aunis.renderer; +import mrjake.aunis.AunisProps; import mrjake.aunis.loader.ElementEnum; import mrjake.aunis.loader.model.ModelLoader; import mrjake.aunis.stargate.network.SymbolMilkyWayEnum; import mrjake.aunis.tileentity.DHDTile; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.init.Blocks; +import net.minecraft.util.math.BlockPos; public class DHDRenderer extends TileEntitySpecialRenderer { @@ -15,8 +18,13 @@ public void render(DHDTile te, double x, double y, double z, float partialTicks, if (rendererState != null) { GlStateManager.pushMatrix(); + GlStateManager.translate(x, y, z); - GlStateManager.translate(x+0.5, y, z+0.5); + if (te.getWorld().getBlockState(te.getPos()).getActualState(te.getWorld(), te.getPos()).getValue(AunisProps.SNOWY)) { + BlockRenderer.render(getWorld(), new BlockPos(0, 0, 0), Blocks.SNOW_LAYER.getDefaultState()); + } + + GlStateManager.translate(0.5, 0, 0.5); GlStateManager.rotate(rendererState.horizontalRotation, 0, 1, 0); ElementEnum.MILKYWAY_DHD.bindTextureAndRender(rendererState.getBiomeOverlay()); @@ -26,6 +34,7 @@ public void render(DHDTile te, double x, double y, double z, float partialTicks, ModelLoader.getModel(symbol.modelResource).render(); } + GlStateManager.popMatrix(); rendererState.iterate(getWorld(), partialTicks); diff --git a/src/main/java/mrjake/aunis/renderer/stargate/StargateAbstractRenderer.java b/src/main/java/mrjake/aunis/renderer/stargate/StargateAbstractRenderer.java index 49b6e555..d19a9515 100644 --- a/src/main/java/mrjake/aunis/renderer/stargate/StargateAbstractRenderer.java +++ b/src/main/java/mrjake/aunis/renderer/stargate/StargateAbstractRenderer.java @@ -18,10 +18,8 @@ import mrjake.aunis.util.AunisAxisAlignedBB; import mrjake.aunis.util.FacingToRotation; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.OpenGlHelper; -import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; @@ -68,15 +66,11 @@ public void render(StargateAbstractBaseTile te, double x, double y, double z, fl renderKawoosh(rendererState, partialTicks); } - else { - bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE); - + else { GlStateManager.enableBlend(); GlStateManager.blendFunc(GL11.GL_CONSTANT_ALPHA, GL11.GL_ONE_MINUS_CONSTANT_ALPHA); GL14.glBlendColor(0, 0, 0, 0.7f); - - Minecraft.getMinecraft().entityRenderer.disableLightmap(); - + for (Map.Entry entry : getMemberBlockStates(te.getMergeHelper(), rendererState.facing).entrySet()) { BlockPos pos = entry.getKey().rotate(FacingToRotation.get(rendererState.facing)); @@ -85,7 +79,6 @@ public void render(StargateAbstractBaseTile te, double x, double y, double z, fl BlockRenderer.render(getWorld(), pos, entry.getValue()); } - Minecraft.getMinecraft().entityRenderer.enableLightmap(); GlStateManager.disableBlend(); } diff --git a/src/main/java/mrjake/aunis/stargate/merging/StargateClassicMergeHelper.java b/src/main/java/mrjake/aunis/stargate/merging/StargateClassicMergeHelper.java index fb9aac37..0fd7ac81 100644 --- a/src/main/java/mrjake/aunis/stargate/merging/StargateClassicMergeHelper.java +++ b/src/main/java/mrjake/aunis/stargate/merging/StargateClassicMergeHelper.java @@ -3,10 +3,7 @@ import mrjake.aunis.AunisProps; import mrjake.aunis.block.stargate.StargateMilkyWayBaseBlock; import mrjake.aunis.block.stargate.StargateMilkyWayMemberBlock; -import mrjake.aunis.packet.AunisPacketHandler; -import mrjake.aunis.packet.StateUpdatePacketToClient; import mrjake.aunis.stargate.EnumMemberVariant; -import mrjake.aunis.state.StateTypeEnum; import mrjake.aunis.tileentity.stargate.StargateClassicMemberTile; import mrjake.aunis.tileentity.stargate.StargateMilkyWayMemberTile; import mrjake.aunis.util.FacingToRotation; @@ -17,7 +14,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; public abstract class StargateClassicMergeHelper extends StargateAbstractMergeHelper { @@ -41,12 +37,13 @@ protected void updateMemberMergeStatus(World world, BlockPos checkPos, BlockPos ItemStack camoStack = memberTile.getCamoItemStack(); if (camoStack != null) { InventoryHelper.spawnItemStack(world, checkPos.getX(), checkPos.getY(), checkPos.getZ(), camoStack); + } + + if (memberTile.getCamoState() != null) { memberTile.setCamoState(null); - - TargetPoint point = new TargetPoint(world.provider.getDimension(), checkPos.getX(), checkPos.getY(), checkPos.getZ(), 512); - AunisPacketHandler.INSTANCE.sendToAllTracking(new StateUpdatePacketToClient(checkPos, StateTypeEnum.CAMO_STATE, memberTile.getState(StateTypeEnum.CAMO_STATE)), point); } + // This also sets merge status memberTile.setBasePos(shouldBeMerged ? basePos : null); world.setBlockState(checkPos, state.withProperty(AunisProps.RENDER_BLOCK, !shouldBeMerged), 3); diff --git a/src/main/java/mrjake/aunis/tileentity/stargate/StargateClassicMemberTile.java b/src/main/java/mrjake/aunis/tileentity/stargate/StargateClassicMemberTile.java index c05b9ce9..321bd5fc 100644 --- a/src/main/java/mrjake/aunis/tileentity/stargate/StargateClassicMemberTile.java +++ b/src/main/java/mrjake/aunis/tileentity/stargate/StargateClassicMemberTile.java @@ -1,5 +1,6 @@ package mrjake.aunis.tileentity.stargate; +import mrjake.aunis.Aunis; import mrjake.aunis.AunisProps; import mrjake.aunis.packet.AunisPacketHandler; import mrjake.aunis.packet.StateUpdatePacketToClient; @@ -62,13 +63,6 @@ public void rotate(Rotation rotation) { */ private boolean isLitUp; - public void syncLightUp() { -// Aunis.info("Syncing light state for chevron"); - if (targetPoint != null) { - AunisPacketHandler.INSTANCE.sendToAllTracking(new StateUpdatePacketToClient(pos, StateTypeEnum.LIGHT_STATE, getState(StateTypeEnum.LIGHT_STATE)), targetPoint); - } - } - public void setLitUp(boolean isLitUp) { boolean sync = isLitUp != this.isLitUp; @@ -76,7 +70,7 @@ public void setLitUp(boolean isLitUp) { markDirty(); if (sync) { - syncLightUp(); + sendState(StateTypeEnum.LIGHT_STATE, getState(StateTypeEnum.LIGHT_STATE)); } } @@ -88,10 +82,25 @@ public boolean isLitUp(IBlockState state) { // --------------------------------------------------------------------------------- private IBlockState camoBlockState; - public void setCamoState(IBlockState doubleSlabState) { - this.camoBlockState = doubleSlabState; + /** + * Should only be called from server. Updates camoBlockState and + * syncs the change to clients. + * + * @param camoBlockState Camouflage block state. + */ + public void setCamoState(IBlockState camoBlockState) { + // Aunis.logger.debug("Setting camo for " + pos + " to " + camoBlockState); + this.camoBlockState = camoBlockState; markDirty(); + + if (!world.isRemote) { + sendState(StateTypeEnum.CAMO_STATE, getState(StateTypeEnum.CAMO_STATE)); + } + + else { + Aunis.logger.warn("Tried to set camoBlockState from client. This won't work!"); + } } public IBlockState getCamoState() { @@ -101,6 +110,10 @@ public IBlockState getCamoState() { public ItemStack getCamoItemStack() { if (camoBlockState != null) { Block block = camoBlockState.getBlock(); + + if (block == Blocks.SNOW_LAYER) + return null; + int quantity = 1; int meta; @@ -165,6 +178,19 @@ public void readFromNBT(NBTTagCompound compound) { // --------------------------------------------------------------------------------- // States + protected void sendState(StateTypeEnum type, State state) { + if (world.isRemote) + return; + + if (targetPoint != null) { + AunisPacketHandler.INSTANCE.sendToAllTracking(new StateUpdatePacketToClient(pos, type, state), targetPoint); + } + + else { + Aunis.logger.debug("targetPoint was null trying to send " + type + " from " + this.getClass().getCanonicalName()); + } + } + @Override public State getState(StateTypeEnum stateType) { switch (stateType) {