From 6aacebcc74dcafcd766e694ae20074ea0dd2e013 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Fri, 19 Jun 2020 20:46:46 +0100 Subject: [PATCH] Stop setting the block each tick to make thorns grow and do it properly in the tile entity and getActualState Also fixes ownerless thorns not dealing damage and removes knockback --- .../wizardry/block/BlockThorns.java | 56 +++++++++--------- .../wizardry/registry/WizardryBlocks.java | 2 +- .../wizardry/spell/ForestOfThorns.java | 21 ++++--- .../tileentity/TileEntityPlayerSaveTimed.java | 50 ---------------- .../wizardry/tileentity/TileEntityThorns.java | 57 +++++++++++++++++++ 5 files changed, 95 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/electroblob/wizardry/tileentity/TileEntityPlayerSaveTimed.java create mode 100644 src/main/java/electroblob/wizardry/tileentity/TileEntityThorns.java diff --git a/src/main/java/electroblob/wizardry/block/BlockThorns.java b/src/main/java/electroblob/wizardry/block/BlockThorns.java index 86abd4fb..10b6b821 100644 --- a/src/main/java/electroblob/wizardry/block/BlockThorns.java +++ b/src/main/java/electroblob/wizardry/block/BlockThorns.java @@ -3,8 +3,9 @@ package electroblob.wizardry.block; import electroblob.wizardry.registry.Spells; import electroblob.wizardry.registry.WizardryBlocks; import electroblob.wizardry.spell.Spell; -import electroblob.wizardry.tileentity.TileEntityPlayerSaveTimed; +import electroblob.wizardry.tileentity.TileEntityThorns; import electroblob.wizardry.util.AllyDesignationSystem; +import electroblob.wizardry.util.EntityUtils; import electroblob.wizardry.util.MagicDamage; import net.minecraft.block.*; import net.minecraft.block.BlockDoublePlant.EnumBlockHalf; @@ -22,8 +23,10 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.DamageSource; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.ChunkCache; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -34,6 +37,7 @@ import java.util.Random; public class BlockThorns extends BlockBush implements ITileEntityProvider { public static final int GROWTH_STAGES = 8; + public static final int GROWTH_STAGE_DURATION = 2; public static final PropertyInteger AGE = PropertyInteger.create("age", 0, GROWTH_STAGES-1); public static final PropertyEnum HALF = PropertyEnum.create("half", EnumBlockHalf.class); @@ -52,22 +56,27 @@ public class BlockThorns extends BlockBush implements ITileEntityProvider { @Override public IBlockState getStateFromMeta(int meta){ - return this.getDefaultState().withProperty(HALF, EnumBlockHalf.values()[meta / GROWTH_STAGES]).withProperty(AGE, meta % GROWTH_STAGES); + return this.getDefaultState().withProperty(HALF, meta == 0 ? EnumBlockHalf.LOWER : EnumBlockHalf.UPPER); } @Override public int getMetaFromState(IBlockState state){ - return state.getValue(HALF).ordinal() * GROWTH_STAGES + state.getValue(AGE); + return state.getValue(HALF).ordinal(); } -// @Override -// public void updateTick(World world, BlockPos pos, IBlockState state, Random rand){ -// -// super.updateTick(world, pos, state, rand); -// -// // Update the state, including on the client, but don't do a block update since it's only visual -// if(state.getValue(AGE) < GROWTH_STAGES-1) world.setBlockState(pos, state.withProperty(AGE, state.getValue(AGE) + 1), 2); -// } + @Override + public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos){ + + if(state.getValue(HALF) == EnumBlockHalf.UPPER) pos = pos.down(); + // Copied from BlockFlowerPot on authority of the Forge docs, which say it needs to be here + TileEntity tileentity = world instanceof ChunkCache ? ((ChunkCache)world).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) : world.getTileEntity(pos); + + if(tileentity instanceof TileEntityThorns){ + return state.withProperty(AGE, ((TileEntityThorns)tileentity).getAge()); + }else{ + return state.withProperty(AGE, 7); + } + } @Override protected BlockStateContainer createBlockState(){ @@ -107,20 +116,6 @@ public class BlockThorns extends BlockBush implements ITileEntityProvider { } } -// @Override -// public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos){ -// // Copied from BlockFlowerPot on authority of the Forge docs, which says this check is necessary -// SoundLoopSpellDispenser tileentity = world instanceof ChunkCache ? ((ChunkCache)world).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) : world.getTileEntity(pos); -// -// if(tileentity instanceof TileEntityPlayerSaveTimed){ -// state = state.withProperty(AGE, Math.min(7, ((TileEntityPlayerSaveTimed)tileentity).timer/2)); -// }else{ -// state = state.withProperty(AGE, 7); -// } -// -// return state; -// } - @Override public void onEntityCollision(World world, BlockPos pos, IBlockState state, Entity entity){ if(applyThornDamage(world, pos, state, entity)){ @@ -134,9 +129,9 @@ public class BlockThorns extends BlockBush implements ITileEntityProvider { TileEntity tileentity = world.getTileEntity(state.getValue(HALF) == EnumBlockHalf.UPPER ? pos.down() : pos); - if(tileentity instanceof TileEntityPlayerSaveTimed){ + if(tileentity instanceof TileEntityThorns){ - EntityLivingBase caster = ((TileEntityPlayerSaveTimed)tileentity).getCaster(); + EntityLivingBase caster = ((TileEntityThorns)tileentity).getCaster(); if(!AllyDesignationSystem.isValidTarget(caster, target)) return false; // Don't attack or slow allies of the caster @@ -145,7 +140,10 @@ public class BlockThorns extends BlockBush implements ITileEntityProvider { } } - if(target.ticksExisted % 20 == 0) target.attackEntityFrom(source, Spells.forest_of_thorns.getProperty(Spell.DAMAGE).floatValue()); + if(target.ticksExisted % 20 == 0){ + float damage = Spells.forest_of_thorns.getProperty(Spell.DAMAGE).floatValue(); + EntityUtils.attackEntityWithoutKnockback(target, source, damage); + } return true; } @@ -157,7 +155,7 @@ public class BlockThorns extends BlockBush implements ITileEntityProvider { @Override public TileEntity createNewTileEntity(World world, int metadata){ - return new TileEntityPlayerSaveTimed(); + return new TileEntityThorns(); } @Override diff --git a/src/main/java/electroblob/wizardry/registry/WizardryBlocks.java b/src/main/java/electroblob/wizardry/registry/WizardryBlocks.java index c5ae30a5..9d9451bc 100644 --- a/src/main/java/electroblob/wizardry/registry/WizardryBlocks.java +++ b/src/main/java/electroblob/wizardry/registry/WizardryBlocks.java @@ -153,7 +153,7 @@ public final class WizardryBlocks { GameRegistry.registerTileEntity(TileEntityMagicLight.class, new ResourceLocation(Wizardry.MODID, "magic_light")); GameRegistry.registerTileEntity(TileEntityTimer.class, new ResourceLocation(Wizardry.MODID, "timer")); GameRegistry.registerTileEntity(TileEntityPlayerSave.class, new ResourceLocation(Wizardry.MODID, "player_save")); - GameRegistry.registerTileEntity(TileEntityPlayerSaveTimed.class, new ResourceLocation(Wizardry.MODID, "player_save_timed")); + GameRegistry.registerTileEntity(TileEntityThorns.class, new ResourceLocation(Wizardry.MODID, "player_save_timed")); GameRegistry.registerTileEntity(TileEntityShrineCore.class, new ResourceLocation(Wizardry.MODID, "shrine_core")); GameRegistry.registerTileEntity(TileEntityBookshelf.class, new ResourceLocation(Wizardry.MODID, "bookshelf")); GameRegistry.registerTileEntity(TileEntityLectern.class, new ResourceLocation(Wizardry.MODID, "lectern")); diff --git a/src/main/java/electroblob/wizardry/spell/ForestOfThorns.java b/src/main/java/electroblob/wizardry/spell/ForestOfThorns.java index f38ef930..daa89c2f 100644 --- a/src/main/java/electroblob/wizardry/spell/ForestOfThorns.java +++ b/src/main/java/electroblob/wizardry/spell/ForestOfThorns.java @@ -4,7 +4,7 @@ import electroblob.wizardry.block.BlockThorns; import electroblob.wizardry.item.SpellActions; import electroblob.wizardry.registry.WizardryBlocks; import electroblob.wizardry.registry.WizardryItems; -import electroblob.wizardry.tileentity.TileEntityPlayerSaveTimed; +import electroblob.wizardry.tileentity.TileEntityThorns; import electroblob.wizardry.util.BlockUtils; import electroblob.wizardry.util.SpellModifiers; import net.minecraft.entity.EntityLiving; @@ -82,19 +82,18 @@ public class ForestOfThorns extends Spell { ((BlockThorns)WizardryBlocks.thorns).placeAt(world, pos, 3); -// for(int i=0; i<2; i++){ + TileEntity tileentity = world.getTileEntity(pos); - TileEntity tileentity = world.getTileEntity(pos); + if(tileentity instanceof TileEntityThorns){ - if(tileentity instanceof TileEntityPlayerSaveTimed){ - ((TileEntityPlayerSaveTimed)tileentity).setLifetime((int)(getProperty(DURATION).floatValue() - * modifiers.get(WizardryItems.duration_upgrade))); - if(caster != null){ - ((TileEntityPlayerSaveTimed)tileentity).setCaster(caster); - } -// ((TileEntityPlayerSaveTimed)tileentity).sync(); + ((TileEntityThorns)tileentity).setLifetime((int)(getProperty(DURATION).floatValue() + * modifiers.get(WizardryItems.duration_upgrade))); + if(caster != null){ + ((TileEntityThorns)tileentity).setCaster(caster); } -// } + + ((TileEntityThorns)tileentity).sync(); + } } } diff --git a/src/main/java/electroblob/wizardry/tileentity/TileEntityPlayerSaveTimed.java b/src/main/java/electroblob/wizardry/tileentity/TileEntityPlayerSaveTimed.java deleted file mode 100644 index 5e3b5534..00000000 --- a/src/main/java/electroblob/wizardry/tileentity/TileEntityPlayerSaveTimed.java +++ /dev/null @@ -1,50 +0,0 @@ -package electroblob.wizardry.tileentity; - -import electroblob.wizardry.block.BlockThorns; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.ITickable; - -public class TileEntityPlayerSaveTimed extends TileEntityPlayerSave implements ITickable { - - public int timer = 0; - public int maxTimer; - - public TileEntityPlayerSaveTimed(){ - this.maxTimer = 600; - } - - @Override - public void update(){ - - timer++; - - if(timer > maxTimer && !this.world.isRemote){ - this.world.destroyBlock(pos, false); - } - - if(timer % 2 == 0 && world.getBlockState(pos).getValue(BlockThorns.AGE) < BlockThorns.GROWTH_STAGES - 1){ - world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockThorns.AGE, world.getBlockState(pos).getValue(BlockThorns.AGE) + 1), 2); - world.setBlockState(pos.up(), world.getBlockState(pos.up()).withProperty(BlockThorns.AGE, world.getBlockState(pos.up()).getValue(BlockThorns.AGE) + 1), 2); - } - } - - public void setLifetime(int lifetime){ - this.maxTimer = lifetime; - } - - @Override - public void readFromNBT(NBTTagCompound tagCompound){ - super.readFromNBT(tagCompound); - timer = tagCompound.getInteger("timer"); - maxTimer = tagCompound.getInteger("maxTimer"); - } - - @Override - public NBTTagCompound writeToNBT(NBTTagCompound tagCompound){ - super.writeToNBT(tagCompound); - tagCompound.setInteger("timer", timer); - tagCompound.setInteger("maxTimer", maxTimer); - return tagCompound; - } - -} diff --git a/src/main/java/electroblob/wizardry/tileentity/TileEntityThorns.java b/src/main/java/electroblob/wizardry/tileentity/TileEntityThorns.java new file mode 100644 index 00000000..dbc5416a --- /dev/null +++ b/src/main/java/electroblob/wizardry/tileentity/TileEntityThorns.java @@ -0,0 +1,57 @@ +package electroblob.wizardry.tileentity; + +import electroblob.wizardry.block.BlockThorns; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.ITickable; + +public class TileEntityThorns extends TileEntityPlayerSave implements ITickable { + + private int ticksExisted = 0; + private int lifetime; + private int age; + + public TileEntityThorns(){ + this.lifetime = 600; + } + + @Override + public void update(){ + + ticksExisted++; + + if(ticksExisted > lifetime && !this.world.isRemote){ + this.world.destroyBlock(pos, false); + } + + if(ticksExisted % BlockThorns.GROWTH_STAGE_DURATION == 0 && age < BlockThorns.GROWTH_STAGES - 1){ + age++; + sync(); // Update displayed block + } + } + + public int getAge(){ + return age; + } + + public void setLifetime(int lifetime){ + this.lifetime = lifetime; + } + + @Override + public void readFromNBT(NBTTagCompound tagCompound){ + super.readFromNBT(tagCompound); + ticksExisted = tagCompound.getInteger("timer"); + lifetime = tagCompound.getInteger("maxTimer"); // Left as maxTimer for backwards compatibility + age = tagCompound.getInteger("age"); + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound tagCompound){ + super.writeToNBT(tagCompound); + tagCompound.setInteger("timer", ticksExisted); + tagCompound.setInteger("maxTimer", lifetime); + tagCompound.setInteger("age", age); + return tagCompound; + } + +}