From 2cc1fc2c328c0e0118b20c83c7fbb5c6068fe3cd Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Thu, 11 Feb 2016 02:02:14 -0500 Subject: [PATCH] Security provider framework; CoFH provider support via Thermal Foundation integration. Not currently loading... --- .../assets/storagedrawers/lang/en_US.lang | 7 ++- .../items/drawer_key_personal_cofh.png | Bin 0 -> 364 bytes ...al.png => drawer_key_personal_default.png} | Bin .../storagedrawers/StorageDrawers.java | 3 + .../api/security/ISecurityProvider.java | 4 ++ .../api/storage/attribute/IProtectable.java | 6 ++ .../storagedrawers/block/BlockController.java | 11 +++- .../storagedrawers/block/BlockDrawers.java | 15 ++++- .../block/tile/TileEntityController.java | 10 +++- .../block/tile/TileEntityDrawers.java | 28 ++++++++- .../storagedrawers/config/ConfigManager.java | 2 + .../integration/IntegrationRegistry.java | 2 + .../integration/ThermalFoundation.java | 40 +++++++++++++ .../CoFHSecurityProvider.java | 40 +++++++++++++ .../storagedrawers/item/ItemPersonalKey.java | 54 +++++++++++++++++- .../storagedrawers/item/ItemTape.java | 2 +- .../security/DefaultSecurityProvider.java | 12 +++- .../security/SecurityManager.java | 11 ++++ .../security/SecurityRegistry.java | 19 ++++++ 19 files changed, 252 insertions(+), 14 deletions(-) create mode 100644 resources/assets/storagedrawers/textures/items/drawer_key_personal_cofh.png rename resources/assets/storagedrawers/textures/items/{drawer_key_personal.png => drawer_key_personal_default.png} (100%) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/integration/ThermalFoundation.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/integration/thermalfoundation/CoFHSecurityProvider.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/security/SecurityRegistry.java diff --git a/resources/assets/storagedrawers/lang/en_US.lang b/resources/assets/storagedrawers/lang/en_US.lang index 478f451..1c089f5 100644 --- a/resources/assets/storagedrawers/lang/en_US.lang +++ b/resources/assets/storagedrawers/lang/en_US.lang @@ -32,8 +32,10 @@ item.storagedrawers.upgradeSorting.name=Sorting Upgrade item.storagedrawers.upgradeSorting.description=For Refined Relocation item.storagedrawers.shroudKey.name=Concealment Key item.storagedrawers.shroudKey.description=Show or hide item labels -item.storagedrawers.personalKey.name=Personal Key -item.storagedrawers.personalKey.description=Protect access from other players +item.storagedrawers.personalKey.default.name=Personal Key +item.storagedrawers.personalKey.default.description=Protect access from other players +item.storagedrawers.personalKey.cofh.name=Personal Key (CoFH) +item.storagedrawers.personalKey.cofh.description=Protect access from other players item.storagedrawers.tape.name=Packing Tape item.storagedrawers.tape.description=Seals drawers for moving @@ -187,3 +189,4 @@ storageDrawers.config.integration.enableThaumcraft=Enable Thaumcraft Integration storageDrawers.config.integration.enableMineTweaker=Enable MineTweaker Integration storageDrawers.config.integration.enableRefinedRelocation=Enable Refined Relocation Integration storageDrawers.config.integration.enableThermalExpansion=Enable Thermal Expansion Integration +storageDrawers.config.integration.enableThermalFoundation=Enable Thermal Foundation Integration diff --git a/resources/assets/storagedrawers/textures/items/drawer_key_personal_cofh.png b/resources/assets/storagedrawers/textures/items/drawer_key_personal_cofh.png new file mode 100644 index 0000000000000000000000000000000000000000..03ae7ebdc8cd6db48f00759453c22cac06019ac2 GIT binary patch literal 364 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Iwh_VCC){ui6xo&c?uz!xv31{ zsVNGE1`2MeNeV^=hGq%|23AJqR)!`Dx(22O28`BmaX@|SC7!;n><^f@SXHEsuDKxy z6yhxKh%9Dc;Q9c lockAttributes = null; @@ -271,7 +272,25 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe @Override public ISecurityProvider getSecurityProvider () { - return null; + return StorageDrawers.securityRegistry.getProvider(securityKey); + } + + @Override + public boolean setSecurityProvider (ISecurityProvider provider) { + if (!StorageDrawers.config.cache.enablePersonalUpgrades) + return false; + + String newKey = (provider == null) ? null : provider.getProviderID(); + if ((newKey != null && !newKey.equals(securityKey)) || (securityKey != null && !securityKey.equals(newKey))) { + securityKey = newKey; + + if (worldObj != null && !worldObj.isRemote) { + markDirty(); + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } + } + + return true; } public boolean shouldHideUpgrades () { @@ -513,6 +532,10 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe if (tag.hasKey("Own")) owner = UUID.fromString(tag.getString("Own")); + securityKey = null; + if (tag.hasKey("Sec")) + securityKey = tag.getString("Sec"); + hideUpgrade = false; if (tag.hasKey("HideUp")) hideUpgrade = tag.getBoolean("HideUp"); @@ -557,6 +580,9 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe if (owner != null) tag.setString("Own", owner.toString()); + if (securityKey != null) + tag.setString("Sec", securityKey); + if (hideUpgrade) tag.setBoolean("HideUp", hideUpgrade); diff --git a/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java b/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java index e346d29..c605877 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java +++ b/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java @@ -75,6 +75,7 @@ public class ConfigManager public boolean enableMineTweakerIntegration; public boolean enableRefinedRelocationIntegration; public boolean enableThermalExpansionIntegration; + public boolean enableThermalFoundationIntegration; public boolean enableTape; public boolean enableFallbackRecipes; public boolean invertShift; @@ -255,6 +256,7 @@ public class ConfigManager cache.enableMineTweakerIntegration = config.get(sectionIntegration.getQualifiedName(), "enableMineTweaker", true).setLanguageKey(LANG_PREFIX + "integration.enableMineTweaker").setRequiresMcRestart(true).getBoolean(); cache.enableRefinedRelocationIntegration = config.get(sectionIntegration.getQualifiedName(), "enableRefinedRelocation", true).setLanguageKey(LANG_PREFIX + "integration.enableRefinedRelocation").setRequiresMcRestart(true).getBoolean(); cache.enableThermalExpansionIntegration = config.get(sectionIntegration.getQualifiedName(), "enableThermalExpansion", true).setLanguageKey(LANG_PREFIX + "integration.enableThermalExpansion").setRequiresMcRestart(true).getBoolean(); + cache.enableThermalFoundationIntegration = config.get(sectionIntegration.getQualifiedName(), "enableThermalFoundation", true).setLanguageKey(LANG_PREFIX + "integration.enableThermalFoundation").setRequiresMcRestart(true).getBoolean(); config.get(sectionBlocksFullDrawers1x1.getQualifiedName(), "enabled", true).setLanguageKey(LANG_PREFIX + "prop.enabled").setRequiresMcRestart(true); config.get(sectionBlocksFullDrawers1x1.getQualifiedName(), "baseStorage", 32).setLanguageKey(LANG_PREFIX + "prop.baseStorage").setRequiresWorldRestart(true); diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/IntegrationRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/integration/IntegrationRegistry.java index be49974..000609c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/IntegrationRegistry.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/IntegrationRegistry.java @@ -30,6 +30,8 @@ public class IntegrationRegistry reg.add(new NotEnoughItems()); if (Loader.isModLoaded("ThermalExpansion") && StorageDrawers.config.cache.enableThermalExpansionIntegration) reg.add(new ThermalExpansion()); + if (Loader.isModLoaded("ThermalFoundation") && StorageDrawers.config.cache.enableThermalFoundationIntegration) + reg.add(new ThermalFoundation()); } private IntegrationRegistry () { diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/ThermalFoundation.java b/src/com/jaquadro/minecraft/storagedrawers/integration/ThermalFoundation.java new file mode 100644 index 0000000..6d716d9 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/ThermalFoundation.java @@ -0,0 +1,40 @@ +package com.jaquadro.minecraft.storagedrawers.integration; + +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.integration.thermalfoundation.CoFHSecurityProvider; +import com.mojang.authlib.GameProfile; + +import java.lang.reflect.Method; + +public class ThermalFoundation extends IntegrationModule +{ + private Class classRegistrySocial; + + private Method methodPlayerHasAccess; + + @Override + public String getModID () { + return "ThermalFoundation"; + } + + @Override + public void init () throws Throwable { + classRegistrySocial = Class.forName("cofh.core.RegistrySocial"); + + methodPlayerHasAccess = classRegistrySocial.getDeclaredMethod("playerHasAccess", String.class, GameProfile.class); + } + + @Override + public void postInit () { + StorageDrawers.securityRegistry.registerProvider(new CoFHSecurityProvider(this)); + } + + public boolean playerHasAccess (String playerName, GameProfile owner) { + try { + return (Boolean) methodPlayerHasAccess.invoke(null, playerName, owner); + } + catch (Throwable t) { + return false; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/thermalfoundation/CoFHSecurityProvider.java b/src/com/jaquadro/minecraft/storagedrawers/integration/thermalfoundation/CoFHSecurityProvider.java new file mode 100644 index 0000000..0ffe93e --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/thermalfoundation/CoFHSecurityProvider.java @@ -0,0 +1,40 @@ +package com.jaquadro.minecraft.storagedrawers.integration.thermalfoundation; + +import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable; +import com.jaquadro.minecraft.storagedrawers.integration.ThermalFoundation; +import com.jaquadro.minecraft.storagedrawers.security.DefaultSecurityProvider; +import com.mojang.authlib.GameProfile; +import net.minecraft.server.MinecraftServer; + +public class CoFHSecurityProvider implements ISecurityProvider +{ + ThermalFoundation foundation; + private DefaultSecurityProvider defaultProvider = new DefaultSecurityProvider(); + + public CoFHSecurityProvider (ThermalFoundation foundation) { + this.foundation = foundation; + } + + @Override + public String getProviderID () { + return "cofh"; + } + + @Override + public boolean hasOwnership (GameProfile profile, IProtectable target) { + return defaultProvider.hasOwnership(profile, target); + } + + @Override + public boolean hasAccess (GameProfile profile, IProtectable target) { + if (target.getOwner() == null) + return true; + + GameProfile ownerProfile = MinecraftServer.getServer().func_152358_ax().func_152652_a(target.getOwner()); + if (ownerProfile == null) + return false; + + return foundation.playerHasAccess(profile.getName(), ownerProfile); + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemPersonalKey.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemPersonalKey.java index e6348a3..d3f8d5f 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemPersonalKey.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemPersonalKey.java @@ -3,26 +3,51 @@ package com.jaquadro.minecraft.storagedrawers.item; import com.google.common.collect.Multimap; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs; +import com.jaquadro.minecraft.storagedrawers.integration.IntegrationRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; +import net.minecraft.util.MathHelper; import net.minecraft.util.StatCollector; import java.util.List; public class ItemPersonalKey extends Item { + public static final String[] iconNames = new String[] { "default", "cofh" }; + + @SideOnly(Side.CLIENT) + private IIcon[] icons; + public ItemPersonalKey (String name) { setUnlocalizedName(name); setCreativeTab(ModCreativeTabs.tabStorageDrawers); - setTextureName(StorageDrawers.MOD_ID + ":drawer_key_personal"); + setHasSubtypes(true); setMaxDamage(0); } + @Override + @SideOnly(Side.CLIENT) + public IIcon getIconFromDamage (int meta) { + return icons[MathHelper.clamp_int(meta, 0, iconNames.length - 1)]; + } + + @Override + public String getUnlocalizedName (ItemStack itemStack) { + int meta = MathHelper.clamp_int(itemStack.getItemDamage(), 0, iconNames.length - 1); + if (iconNames[meta] == null) + return super.getUnlocalizedName(); + + return super.getUnlocalizedName() + "." + iconNames[meta]; + } + @Override @SideOnly(Side.CLIENT) public void addInformation (ItemStack itemStack, EntityPlayer player, List list, boolean par4) { @@ -42,4 +67,31 @@ public class ItemPersonalKey extends Item multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Weapon modifier", (double)2, 0)); return multimap; } + + @Override + @SideOnly(Side.CLIENT) + public void getSubItems (Item item, CreativeTabs creativeTabs, List list) { + list.add(new ItemStack(item, 1, 0)); + if (StorageDrawers.config.cache.enableThermalFoundationIntegration && IntegrationRegistry.instance().isModLoaded("ThermalFoundation")) + list.add(new ItemStack(item, 1, 1)); + } + + @Override + @SideOnly(Side.CLIENT) + public void registerIcons (IIconRegister register) { + icons = new IIcon[iconNames.length]; + + for (int i = 0, n = iconNames.length; i < n; i++) { + if (iconNames[i] != null) + icons[i] = register.registerIcon(StorageDrawers.MOD_ID + ":drawer_key_personal_" + iconNames[i]); + } + } + + public String getSecurityProviderKey (int meta) { + switch (meta) { + case 0: return null; + case 1: return "cofh"; + default: return null; + } + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java index 1145666..73bd98c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java @@ -40,7 +40,7 @@ public class ItemTape extends Item TileEntity tile = world.getTileEntity(x, y, z); if (tile instanceof IProtectable) { - if (!SecurityManager.hasAccess(player.getGameProfile(), (IProtectable)tile)) + if (!SecurityManager.hasOwnership(player.getGameProfile(), (IProtectable)tile)) return false; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java b/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java index 6bea448..832275f 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java @@ -7,10 +7,20 @@ import com.mojang.authlib.GameProfile; public class DefaultSecurityProvider implements ISecurityProvider { @Override - public boolean hasAccess (GameProfile profile, IProtectable target) { + public String getProviderID () { + return null; + } + + @Override + public boolean hasOwnership (GameProfile profile, IProtectable target) { if (target == null || profile == null) return false; return target.getOwner() == null || target.getOwner().equals(profile.getId()); } + + @Override + public boolean hasAccess (GameProfile profile, IProtectable target) { + return hasOwnership(profile, target); + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java index 78f29dd..f0e921a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java +++ b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java @@ -8,6 +8,17 @@ public class SecurityManager { private static ISecurityProvider defaultProvider = new DefaultSecurityProvider(); + public static boolean hasOwnership (GameProfile profile, IProtectable target) { + if (target == null || profile == null) + return false; + + ISecurityProvider provider = target.getSecurityProvider(); + if (provider == null) + provider = defaultProvider; + + return provider.hasOwnership(profile, target); + } + public static boolean hasAccess (GameProfile profile, IProtectable target) { if (target == null || profile == null) return false; diff --git a/src/com/jaquadro/minecraft/storagedrawers/security/SecurityRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityRegistry.java new file mode 100644 index 0000000..8c5a3da --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityRegistry.java @@ -0,0 +1,19 @@ +package com.jaquadro.minecraft.storagedrawers.security; + +import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; + +import java.util.HashMap; +import java.util.Map; + +public class SecurityRegistry +{ + private Map registry = new HashMap(); + + public void registerProvider (ISecurityProvider provider) { + registry.put(provider.getProviderID(), provider); + } + + public ISecurityProvider getProvider (String name) { + return registry.get(name); + } +}