From f1cb7dbbbe9174818c55104a3047691d6893489a Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Sat, 18 Apr 2020 16:12:05 +0100 Subject: [PATCH] Make arcane lock work on both halves of a double chest at once --- .../wizardry/spell/ArcaneLock.java | 41 ++++++++++++------- .../wizardry/util/WizardryUtilities.java | 25 ++++++++++- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/main/java/electroblob/wizardry/spell/ArcaneLock.java b/src/main/java/electroblob/wizardry/spell/ArcaneLock.java index b2c88f5a..a93264e2 100644 --- a/src/main/java/electroblob/wizardry/spell/ArcaneLock.java +++ b/src/main/java/electroblob/wizardry/spell/ArcaneLock.java @@ -47,28 +47,39 @@ public class ArcaneLock extends SpellRay { protected boolean onBlockHit(World world, BlockPos pos, EnumFacing side, Vec3d hit, EntityLivingBase caster, Vec3d origin, int ticksInUse, SpellModifiers modifiers){ if(caster instanceof EntityPlayer){ - - TileEntity tileentity = world.getTileEntity(pos); - if(tileentity != null){ - - if(tileentity.getTileData().hasUniqueId(NBT_KEY)){ - // Unlocking - if(world.getPlayerEntityByUUID(tileentity.getTileData().getUniqueId(NBT_KEY)) == caster){ - NBTExtras.removeUniqueId(tileentity.getTileData(), NBT_KEY); - return true; - } - }else{ - // Locking - tileentity.getTileData().setUniqueId(NBT_KEY, caster.getUniqueID()); - return true; - } + if(toggleLock(world, pos, (EntityPlayer)caster)){ + BlockPos otherHalf = WizardryUtilities.getConnectedChest(world, pos); + if(otherHalf != null) toggleLock(world, otherHalf, (EntityPlayer)caster); + return true; } } return false; } + private boolean toggleLock(World world, BlockPos pos, EntityPlayer player){ + + TileEntity tileentity = world.getTileEntity(pos); + + if(tileentity != null){ + + if(tileentity.getTileData().hasUniqueId(NBT_KEY)){ + // Unlocking + if(world.getPlayerEntityByUUID(tileentity.getTileData().getUniqueId(NBT_KEY)) == player){ + NBTExtras.removeUniqueId(tileentity.getTileData(), NBT_KEY); + return true; + } + }else{ + // Locking + tileentity.getTileData().setUniqueId(NBT_KEY, player.getUniqueID()); + return true; + } + } + + return false; + } + @Override protected boolean onMiss(World world, EntityLivingBase caster, Vec3d origin, Vec3d direction, int ticksInUse, SpellModifiers modifiers){ return false; diff --git a/src/main/java/electroblob/wizardry/util/WizardryUtilities.java b/src/main/java/electroblob/wizardry/util/WizardryUtilities.java index b5b97d35..870803a9 100644 --- a/src/main/java/electroblob/wizardry/util/WizardryUtilities.java +++ b/src/main/java/electroblob/wizardry/util/WizardryUtilities.java @@ -9,6 +9,7 @@ import electroblob.wizardry.item.ISpellCastingItem; import electroblob.wizardry.spell.Spell; import net.minecraft.block.Block; import net.minecraft.block.BlockCactus; +import net.minecraft.block.BlockChest; import net.minecraft.block.BlockLog; import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.IBlockState; @@ -39,7 +40,6 @@ import javax.annotation.Nullable; import java.util.*; import java.util.function.BiPredicate; import java.util.function.Predicate; -import java.util.stream.Collectors; /** * "Where do you put random but useful bits and pieces? {@code WizardryUtilities} of course - the 'stuff that doesn't @@ -307,6 +307,29 @@ public final class WizardryUtilities { || Settings.containsMetaBlock(Wizardry.settings.treeBlocks, world.getBlockState(pos)); } + /** + * Given the position of one half of a double chest, returns the position of the other half of that double chest. + * @param world The world in which the chest is located + * @param pos The position of one half of the double chest + * @return The position of the other half of the double chest, or null if the given position is not part of a + * double chest. + */ + public static BlockPos getConnectedChest(World world, BlockPos pos){ + + Block block = world.getBlockState(pos).getBlock(); + + if(block instanceof BlockChest){ + for(EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL){ + BlockPos pos1 = pos.offset(enumfacing); + if(world.getBlockState(pos1).getBlock() == block){ + return pos1; + } + } + } + + return null; + } + /** * Finds the nearest floor level in the given direction from the given position, * within the range specified. This is a shorthand for