Fixes to the security feature

This commit is contained in:
Raoul Van den Berge
2016-12-17 01:05:51 +01:00
parent dbb7d2ec5c
commit 2e9905055a
8 changed files with 76 additions and 22 deletions

View File

@@ -7,9 +7,9 @@ import java.util.UUID;
*/
public interface ISecurityCard {
/**
* @return the UUID that this security card is bound to
* @return the owner of this card
*/
UUID getBound();
UUID getOwner();
/**
* @param permission the permission to check for

View File

@@ -38,7 +38,7 @@ public class NetworkItemWirelessCraftingMonitor implements INetworkItem {
return false;
}
if (!network.getSecurityManager().hasPermission(Permission.AUTOCRAFTING, player)) {
if (!network.getSecurityManager().hasPermission(Permission.MODIFY, player) || !network.getSecurityManager().hasPermission(Permission.AUTOCRAFTING, player)) {
RSUtils.sendNoPermissionMessage(player);
return false;

View File

@@ -8,11 +8,11 @@ import java.util.Map;
import java.util.UUID;
public class SecurityCard implements ISecurityCard {
private UUID bound;
private UUID owner;
private Map<Permission, Boolean> permissions = new HashMap<>();
public SecurityCard(UUID bound) {
this.bound = bound;
public SecurityCard(UUID owner) {
this.owner = owner;
}
public Map<Permission, Boolean> getPermissions() {
@@ -20,8 +20,8 @@ public class SecurityCard implements ISecurityCard {
}
@Override
public UUID getBound() {
return bound;
public UUID getOwner() {
return owner;
}
@Override

View File

@@ -40,9 +40,9 @@ public class SecurityManager implements ISecurityManager {
cards.clear();
for (INetworkNode node : network.getNodeGraph().all()) {
if (node instanceof ISecurityCardContainer) {
if (node instanceof ISecurityCardContainer && node.canUpdate()) {
for (ISecurityCard card : ((ISecurityCardContainer) node).getCards()) {
cards.put(card.getBound(), card);
cards.put(card.getOwner(), card);
}
}
}

View File

@@ -1,10 +1,13 @@
package com.raoulvdberge.refinedstorage.block;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSGui;
import com.raoulvdberge.refinedstorage.api.network.security.Permission;
import com.raoulvdberge.refinedstorage.tile.TileSecurityManager;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
@@ -21,10 +24,23 @@ public class BlockSecurityManager extends BlockNode {
return new TileSecurityManager();
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, placer, stack);
if (!world.isRemote && placer instanceof EntityPlayer) {
((TileSecurityManager) world.getTileEntity(pos)).setOwner(((EntityPlayer) placer).getGameProfile().getId());
}
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {
tryOpenNetworkGui(RSGui.SECURITY_MANAGER, player, world, pos, side, Permission.MODIFY, Permission.SECURITY);
if (player.getGameProfile().getId().equals(((TileSecurityManager) world.getTileEntity(pos)).getOwner())) {
player.openGui(RS.INSTANCE, RSGui.SECURITY_MANAGER, world, pos.getX(), pos.getY(), pos.getZ());
} else {
tryOpenNetworkGui(RSGui.SECURITY_MANAGER, player, world, pos, side, Permission.MODIFY, Permission.SECURITY);
}
}
return true;

View File

@@ -15,8 +15,8 @@ import java.util.List;
import java.util.UUID;
public class ItemSecurityCard extends ItemBase {
private static final String NBT_BOUND = "Bound";
private static final String NBT_BOUND_NAME = "BoundName";
private static final String NBT_OWNER = "Owner";
private static final String NBT_OWNER_NAME = "OwnerName";
private static final String NBT_PERMISSION = "Permission_%d";
public ItemSecurityCard() {
@@ -32,17 +32,17 @@ public class ItemSecurityCard extends ItemBase {
if (!world.isRemote) {
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setString(NBT_BOUND, player.getGameProfile().getId().toString());
stack.getTagCompound().setString(NBT_BOUND_NAME, player.getGameProfile().getName());
stack.getTagCompound().setString(NBT_OWNER, player.getGameProfile().getId().toString());
stack.getTagCompound().setString(NBT_OWNER_NAME, player.getGameProfile().getName());
}
return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
}
@Nullable
public static UUID getBound(ItemStack stack) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_BOUND)) {
return UUID.fromString(stack.getTagCompound().getString(NBT_BOUND));
public static UUID getOwner(ItemStack stack) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_OWNER)) {
return UUID.fromString(stack.getTagCompound().getString(NBT_OWNER));
}
return null;
@@ -70,8 +70,8 @@ public class ItemSecurityCard extends ItemBase {
public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced) {
super.addInformation(stack, player, tooltip, advanced);
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_BOUND_NAME)) {
tooltip.add(I18n.format("item.refinedstorage:security_card.bound", stack.getTagCompound().getString(NBT_BOUND_NAME)));
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_OWNER_NAME)) {
tooltip.add(I18n.format("item.refinedstorage:security_card.owner", stack.getTagCompound().getString(NBT_OWNER_NAME)));
}
}
}

View File

@@ -3,6 +3,7 @@ package com.raoulvdberge.refinedstorage.tile;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSItems;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.network.security.ISecurityCard;
import com.raoulvdberge.refinedstorage.api.network.security.ISecurityCardContainer;
import com.raoulvdberge.refinedstorage.api.network.security.Permission;
@@ -12,12 +13,17 @@ import com.raoulvdberge.refinedstorage.inventory.ItemValidatorBasic;
import com.raoulvdberge.refinedstorage.item.ItemSecurityCard;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class TileSecurityManager extends TileNode implements ISecurityCardContainer {
private static final String NBT_OWNER = "Owner";
private List<ISecurityCard> actualCards = new ArrayList<>();
private ItemHandlerBasic cards = new ItemHandlerBasic(9 * 2, this, new ItemValidatorBasic(RSItems.SECURITY_CARD)) {
@@ -36,6 +42,9 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
};
private ItemHandlerBasic editCard = new ItemHandlerBasic(1, this, new ItemValidatorBasic(RSItems.SECURITY_CARD));
@Nullable
private UUID owner;
@Override
public int getEnergyUsage() {
int usage = RS.INSTANCE.config.securityManagerUsage;
@@ -58,6 +67,17 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
super.update();
}
public void setOwner(@Nullable UUID owner) {
this.owner = owner;
markDirty();
}
@Nullable
public UUID getOwner() {
return owner;
}
@Override
public void updateNode() {
// NO OP
@@ -70,7 +90,7 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
ItemStack stack = cards.getStackInSlot(i);
if (!stack.isEmpty()) {
UUID uuid = ItemSecurityCard.getBound(stack);
UUID uuid = ItemSecurityCard.getOwner(stack);
if (uuid == null) {
continue;
@@ -91,6 +111,10 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
public void read(NBTTagCompound tag) {
super.read(tag);
if (tag.hasKey(NBT_OWNER)) {
owner = UUID.fromString(tag.getString(NBT_OWNER));
}
RSUtils.readItems(cards, 0, tag);
RSUtils.readItems(editCard, 1, tag);
}
@@ -99,12 +123,21 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
public NBTTagCompound write(NBTTagCompound tag) {
super.write(tag);
if (owner != null) {
tag.setString(NBT_OWNER, owner.toString());
}
RSUtils.writeItems(cards, 0, tag);
RSUtils.writeItems(editCard, 1, tag);
return tag;
}
@Override
public void onConnectionChange(INetworkMaster network, boolean state) {
network.getSecurityManager().rebuild();
}
public ItemHandlerBasic getCardsItems() {
return cards;
}
@@ -125,4 +158,9 @@ public class TileSecurityManager extends TileNode implements ISecurityCardContai
public List<ISecurityCard> getCards() {
return actualCards;
}
@Override
public IItemHandler getDrops() {
return new CombinedInvWrapper(cards, editCard);
}
}

View File

@@ -261,4 +261,4 @@ item.refinedstorage:wrench.mode=Mode: %s
item.refinedstorage:wrench.mode.0=Rotation
item.refinedstorage:wrench.mode.1=Configuration
item.refinedstorage:security_card.name=Security Card
item.refinedstorage:security_card.bound=Bound to: %s
item.refinedstorage:security_card.owner=Bound to: %s