Make arcane lock work on both halves of a double chest at once

This commit is contained in:
Electroblob77
2020-04-18 16:12:05 +01:00
parent 5f8477a4ad
commit f1cb7dbbbe
2 changed files with 50 additions and 16 deletions
@@ -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;
@@ -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;
/**
* <i>"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