diff options
Diffstat (limited to 'src/Java/miscutil/core')
-rw-r--r-- | src/Java/miscutil/core/handler/XEventHandler.java | 61 | ||||
-rw-r--r-- | src/Java/miscutil/core/lib/CORE.java | 3 | ||||
-rw-r--r-- | src/Java/miscutil/core/util/PlayerCache.java | 107 |
3 files changed, 171 insertions, 0 deletions
diff --git a/src/Java/miscutil/core/handler/XEventHandler.java b/src/Java/miscutil/core/handler/XEventHandler.java new file mode 100644 index 0000000000..3742f29e47 --- /dev/null +++ b/src/Java/miscutil/core/handler/XEventHandler.java @@ -0,0 +1,61 @@ +package miscutil.core.handler; + +import gregtech.api.interfaces.metatileentity.IMetaTileEntity; +import gregtech.api.metatileentity.BaseMetaTileEntity; +import gregtech.api.metatileentity.BaseTileEntity; +import miscutil.core.util.Utils; +import miscutil.gregtech.metatileentity.implementations.base.GregtechMetaSafeBlockBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.event.world.BlockEvent.BreakEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +public class XEventHandler { + @SubscribeEvent + public void onBreakBlock(BreakEvent event) { + TileEntity entity = event.world.getTileEntity(event.x, event.y, event.z); + EntityPlayer playerInternal = event.getPlayer(); + + try{ + if (entity instanceof BaseTileEntity){ + IMetaTileEntity X = ((BaseMetaTileEntity)entity).getMetaTileEntity(); + if (X instanceof GregtechMetaSafeBlockBase){ + + String ownerUUID = ((GregtechMetaSafeBlockBase)X).ownerUUID; + String accessorUUID = playerInternal.getUniqueID().toString(); + Utils.LOG_WARNING("Owner UUID: "+ownerUUID); + Utils.LOG_WARNING("Accessor UUID: "+accessorUUID); + + if (((GregtechMetaSafeBlockBase)X).bUnbreakable){ + if (accessorUUID.equals(ownerUUID)){ + Utils.messagePlayer(playerInternal, "Since you own this block, it has been destroyed."); + event.setCanceled(false); + } + else { + Utils.messagePlayer(playerInternal, "Since you do not own this block, it has not been destroyed."); + event.setCanceled(true); + } + // + } + } + } + } + catch (NullPointerException e) { + System.out.print("Caught a NullPointerException involving Safe Blocks. Cause: "+e.getCause()); + } + + + + + + + + + + + + + + + } +} diff --git a/src/Java/miscutil/core/lib/CORE.java b/src/Java/miscutil/core/lib/CORE.java index 12be9e9a03..4acd30d4d8 100644 --- a/src/Java/miscutil/core/lib/CORE.java +++ b/src/Java/miscutil/core/lib/CORE.java @@ -1,5 +1,7 @@ package miscutil.core.lib; +import java.util.Map; + import miscutil.core.creativetabs.AddToCreativeTab; public class CORE { @@ -10,6 +12,7 @@ public class CORE { public static final boolean DEBUG = true; public static final boolean LOAD_ALL_CONTENT = false; public static final int GREG_FIRST_ID = 760; + public static Map PlayerCache; public static final Class<AddToCreativeTab> TAB = AddToCreativeTab.class; diff --git a/src/Java/miscutil/core/util/PlayerCache.java b/src/Java/miscutil/core/util/PlayerCache.java new file mode 100644 index 0000000000..40f44e003a --- /dev/null +++ b/src/Java/miscutil/core/util/PlayerCache.java @@ -0,0 +1,107 @@ +package miscutil.core.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Properties; + +import miscutil.core.lib.CORE; + +public class PlayerCache { + + private static final File cache = new File("PlayerCache.dat"); + + public static void createPropertiesFile(String playerName, String playerUUIDasString) { + try { + Properties props = new Properties(); + props.setProperty(playerName, playerUUIDasString); + OutputStream out = new FileOutputStream(cache); + props.store(out, "Player Cache."); + } + catch (Exception e ) { + e.printStackTrace(); + } + } + + public static void appendParamChanges(String playerName, String playerUUIDasString) { + try { + + Properties properties = new Properties(); + try { + Utils.LOG_WARNING("Attempting to load "+cache.getName()); + properties.load(new FileInputStream(cache)); + } catch (IOException e) { + Utils.LOG_WARNING("No PlayerCache file found, creating one."); + createPropertiesFile(playerName, playerUUIDasString); + } + + properties.setProperty(playerName, playerUUIDasString); + FileOutputStream fr=new FileOutputStream(cache); + properties.store(fr, "Player Cache."); + fr.close(); + } + catch (Exception e ) { + e.printStackTrace(); + } + } + + /** + * Reads a "properties" file, and returns it as a Map + * (a collection of key/value pairs). + * + * Credit due to Alvin Alexander - http://alvinalexander.com/java/java-properties-file-map-example?nocache=1#comment-8215 + * Changed slightly as the filename and delimiter are constant in my case. + * + * @param filename The properties filename to read. + * @param delimiter The string (or character) that separates the key + * from the value in the properties file. + * @return The Map that contains the key/value pairs. + * @throws Exception + */ + private static Map<String, String> readPropertiesFileAsMap() throws Exception { + String filename = cache.getName(); + String delimiter = ":"; + @SuppressWarnings({ "rawtypes", "unchecked" }) + Map<String, String> map = new HashMap(); + BufferedReader reader = new BufferedReader(new FileReader(filename)); + String line; + while ((line = reader.readLine()) != null) + { + if (line.trim().length()==0) continue; + if (line.charAt(0)=='#') continue; + // assumption here is that proper lines are like "String : <a href="http://xxx.yyy.zzz/foo/bar"" title="http://xxx.yyy.zzz/foo/bar"">http://xxx.yyy.zzz/foo/bar"</a>, + // and the ":" is the delimiter + int delimPosition = line.indexOf(delimiter); + String key = line.substring(0, delimPosition-1).trim(); + String value = line.substring(delimPosition+1).trim(); + map.put(key, value); + } + reader.close(); + CORE.PlayerCache = map; + return map; + } + + public static String lookupPlayerByUUID(String UUID){ + Map<String, String> map = null; + try { + map = readPropertiesFileAsMap(); + } catch (Exception e) { + Utils.LOG_ERROR("Caught Exception"+e.toString()); + e.printStackTrace(); + } + for (Entry<String, String> entry : map.entrySet()) { + if (Objects.equals(UUID, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } +} |