Add protection support to tape and (manual) controller interaction.

This commit is contained in:
Justin Aquadro committed 2016-02-08 23:37:16 -05:00
1 parent f94d25fc68
commit 6b34cefaf2
5 files changed
+89 -16

No files matched your search

@@ -1,8 +1,17 @@
package com.jaquadro.minecraft.storagedrawers.api.storage.attribute;
/**
* Created by Justin on 2/8/2016.
*/
import java.util.UUID;
public interface IProtectable
{
/**
* Gets the owner of the drawer, or null if there is no owner.
*/
UUID getOwner ();
/**
* Sets the owner of the drawer. Set to null to set no owner.
* @return false if the operation is not supported, true otherwise.
*/
boolean setOwner (UUID owner);
}
@@ -121,12 +121,17 @@ public class BlockController extends BlockContainer implements INetworked
if (item != null && item.getItem() != null) {
if (item.getItem() == ModItems.shroudKey) {
if (!world.isRemote)
te.toggleShroud();
te.toggleShroud(player.getPersistentID());
return true;
}
else if (item.getItem() == ModItems.upgradeLock) {
if (!world.isRemote)
te.toggleLock(EnumSet.allOf(LockAttribute.class), LockAttribute.LOCK_POPULATED);
te.toggleLock(EnumSet.allOf(LockAttribute.class), LockAttribute.LOCK_POPULATED, player.getPersistentID());
return true;
}
else if (item.getItem() == ModItems.personalKey) {
if (!world.isRemote)
te.toggleProtection(player.getPersistentID());
return true;
}
}
@@ -3,10 +3,7 @@ package com.jaquadro.minecraft.storagedrawers.block.tile;
import com.jaquadro.minecraft.storagedrawers.StorageDrawers;
import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory;
import com.jaquadro.minecraft.storagedrawers.api.storage.*;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ILockable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IShroudable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*;
import com.jaquadro.minecraft.storagedrawers.block.BlockSlave;
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaListRegistry;
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaRegistry;
@@ -158,7 +155,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
if (!dumpInventory) {
ItemStack currentStack = player.inventory.getCurrentItem();
if (currentStack != null) {
count = insertItems(currentStack);
count = insertItems(currentStack, player.getPersistentID());
if (currentStack.stackSize == 0)
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
}
@@ -167,7 +164,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
for (int i = 0, n = player.inventory.getSizeInventory(); i < n; i++) {
ItemStack subStack = player.inventory.getStackInSlot(i);
if (subStack != null) {
count += insertItems(subStack);
count += insertItems(subStack, player.getPersistentID());
if (subStack.stackSize == 0)
player.inventory.setInventorySlotContents(i, null);
}
@@ -183,10 +180,17 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
return count;
}
private int insertItems (ItemStack stack) {
private int insertItems (ItemStack stack, UUID owner) {
int itemsLeft = stack.stackSize;
for (int slot : enumerateDrawersForInsertion(stack, false)) {
IDrawerGroup group = getGroupForDrawerSlot(slot);
if (group instanceof IProtectable) {
IProtectable protectable = (IProtectable)group;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
continue;
}
IDrawer drawer = getDrawer(slot);
ItemStack itemProto = drawer.getStoredItemPrototype();
if (itemProto == null)
@@ -227,7 +231,34 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
return remainder;
}
public void toggleShroud () {
public void toggleProtection (UUID owner) {
IProtectable template = null;
UUID state = null;
for (StorageRecord record : storage.values()) {
if (record.storage == null)
continue;
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
continue;
if (template == null) {
template = protectable;
if (template.getOwner() == null)
state = owner;
else
state = null;
}
protectable.setOwner(state);
}
}
}
public void toggleShroud (UUID owner) {
IShroudable template = null;
boolean state = false;
@@ -235,6 +266,12 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
if (record.storage == null)
continue;
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
continue;
}
for (int i = 0, n = record.storage.getDrawerCount(); i < n; i++) {
if (!record.storage.isDrawerEnabled(i))
continue;
@@ -254,7 +291,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
}
}
public void toggleLock (EnumSet<LockAttribute> attributes, LockAttribute key) {
public void toggleLock (EnumSet<LockAttribute> attributes, LockAttribute key, UUID owner) {
ILockable template = null;
boolean state = false;
@@ -262,6 +299,12 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
if (record.storage == null)
continue;
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
continue;
}
if (record.storage instanceof ILockable) {
ILockable lockableStorage = (ILockable)record.storage;
if (template == null) {
@@ -5,6 +5,7 @@ import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroupInteractive;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ILockable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ISealable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.config.ConfigManager;
@@ -35,7 +36,7 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.UUID;
public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawerGroupInteractive, ISidedInventory, IUpgradeProvider, ILockable, ISealable
public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawerGroupInteractive, ISidedInventory, IUpgradeProvider, ILockable, ISealable, IProtectable
{
private IDrawer[] drawers;
private IDrawerInventory inventory;
@@ -242,6 +243,7 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe
}
}
@Override
public UUID getOwner () {
if (!StorageDrawers.config.cache.enablePersonalUpgrades)
return null;
@@ -249,7 +251,11 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe
return owner;
}
public void setOwner (UUID owner) {
@Override
public boolean setOwner (UUID owner) {
if (!StorageDrawers.config.cache.enablePersonalUpgrades)
return false;
if ((this.owner != null && !this.owner.equals(owner)) || (owner != null && !owner.equals(this.owner))) {
this.owner = owner;
@@ -258,6 +264,8 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
return true;
}
public boolean shouldHideUpgrades () {
@@ -1,6 +1,7 @@
package com.jaquadro.minecraft.storagedrawers.item;
import com.jaquadro.minecraft.storagedrawers.StorageDrawers;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ISealable;
import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs;
import cpw.mods.fml.relauncher.Side;
@@ -37,6 +38,13 @@ public class ItemTape extends Item
return false;
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof IProtectable) {
IProtectable protectable = (IProtectable)tile;
if (protectable.getOwner() != null && !protectable.getOwner().equals(player.getPersistentID()))
return false;
}
if (tile instanceof ISealable) {
ISealable tileseal = (ISealable) tile;
if (tileseal.isSealed())