diff options
Diffstat (limited to 'src/Java/miscutil/core/util/player')
-rw-r--r-- | src/Java/miscutil/core/util/player/PlayerCache.java | 127 | ||||
-rw-r--r-- | src/Java/miscutil/core/util/player/UtilsMining.java | 180 |
2 files changed, 307 insertions, 0 deletions
diff --git a/src/Java/miscutil/core/util/player/PlayerCache.java b/src/Java/miscutil/core/util/player/PlayerCache.java new file mode 100644 index 0000000000..5969766556 --- /dev/null +++ b/src/Java/miscutil/core/util/player/PlayerCache.java @@ -0,0 +1,127 @@ +package miscutil.core.util.player; + +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; +import miscutil.core.util.Utils; + +public class PlayerCache { + + private static final File cache = new File("PlayerCache.dat"); + + public static final void initCache() { + if (CORE.PlayerCache == null || CORE.PlayerCache.equals(null)){ + try { + CORE.PlayerCache = PlayerCache.readPropertiesFileAsMap(); + Utils.LOG_INFO("Loaded PlayerCache.dat"); + } catch (Exception e) { + Utils.LOG_INFO("Failed to initialise PlayerCache.dat"); + PlayerCache.createPropertiesFile("CACHE_FILE_=", "THIS_CONTAINS_PLAYERDATA"); + //e.printStackTrace(); + } + } + } + + 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."); + Utils.LOG_INFO("Created an empty PlayerCache.dat for future use."); + } + catch (Exception e ) { + e.printStackTrace(); + } + } + + public static void appendParamChanges(String playerName, String playerUUIDasString) { + Properties properties = new Properties(); + try { + Utils.LOG_WARNING("Attempting to load "+cache.getName()); + properties.load(new FileInputStream(cache)); + if (properties == null || properties.equals(null)){ + Utils.LOG_WARNING("Null properties"); + } + else { + Utils.LOG_WARNING("Loaded PlayerCache.dat"); + properties.setProperty(playerName+"_", playerUUIDasString); + FileOutputStream fr=new FileOutputStream(cache); + properties.store(fr, "Player Cache."); + fr.close(); + } + + } catch (IOException e) { + Utils.LOG_WARNING("No PlayerCache file found, creating one."); + createPropertiesFile(playerName, playerUUIDasString); + } + } + + /** + * 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 + */ + public static Map<String, String> readPropertiesFileAsMap() throws Exception { + String delimiter = "="; + @SuppressWarnings({ "rawtypes", "unchecked" }) + Map<String, String> map = new HashMap(); + BufferedReader reader = new BufferedReader(new FileReader(cache)); + 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){ + try { + Map<String, String> map = null; + try { + map = readPropertiesFileAsMap(); + } catch (Exception e) { + Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString()); + //e.printStackTrace(); + } + for (Entry<String, String> entry : map.entrySet()) { + if (Objects.equals(UUID, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } catch (NullPointerException e) { + Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString()); + //e.printStackTrace(); + } + return null; +} +} diff --git a/src/Java/miscutil/core/util/player/UtilsMining.java b/src/Java/miscutil/core/util/player/UtilsMining.java new file mode 100644 index 0000000000..81e69c1aff --- /dev/null +++ b/src/Java/miscutil/core/util/player/UtilsMining.java @@ -0,0 +1,180 @@ +package miscutil.core.util.player; + +import miscutil.core.util.Utils; +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +public class UtilsMining { + + private static boolean durabilityDamage = false; + private static ItemStack stack; + + public static Boolean canPickaxeBlock(Block currentBlock, World currentWorld){ + String correctTool = ""; + if (!currentWorld.isRemote){ + try { + correctTool = currentBlock.getHarvestTool(0); + //Utils.LOG_WARNING(correctTool); + if (correctTool.equals("pickaxe")){ + return true;} + } catch (NullPointerException e){ + return false;} + } + return false; + } + + private static void removeBlockAndDropAsItem(World world, int X, int Y, int Z){ + try { + Block block = world.getBlock(X, Y, Z); + if (canPickaxeBlock(block, world)){ + if((block != Blocks.bedrock) && (block.getBlockHardness(world, X, Y, Z) != -1) && (block.getBlockHardness(world, X, Y, Z) <= 100) && (block != Blocks.water) && (block != Blocks.lava)){ + block.dropBlockAsItem(world, X, Y, Z, world.getBlockMetadata(X, Y, Z), 0); + world.setBlockToAir(X, Y, Z); + + } + else { + Utils.LOG_WARNING("Incorrect Tool for mining this block."); + } + } + } catch (NullPointerException e){ + + } + } + + public static void customMine(World world, String FACING, EntityPlayer aPlayer){ + + float DURABILITY_LOSS = 0; + if (!world.isRemote){ + int X = 0; + int Y = 0; + int Z = 0; + + if (FACING.equals("below") || FACING.equals("above")){ + + //Set Player Facing + X = (int) aPlayer.posX; + Utils.LOG_WARNING("Setting Variable X: "+X); + if (FACING.equals("above")){ + Z = (int) aPlayer.posY + 1; + Utils.LOG_WARNING("Setting Variable Y: "+Y); + } + else { + Z = (int) aPlayer.posY - 1; + Utils.LOG_WARNING("Setting Variable Y: "+Y);} + Z = (int) aPlayer.posZ; + Utils.LOG_WARNING("Setting Variable Z: "+Z); + + DURABILITY_LOSS = 0; + for(int i = -2; i < 3; i++) { + for(int j = -2; j < 3; j++) { + for(int k = -2; k < 3; k++) { +/*// float dur = calculateDurabilityLoss(world, X + i, Y + k, Z + j); +// DURABILITY_LOSS = (DURABILITY_LOSS + dur); +// Utils.LOG_WARNING("Added Loss: "+dur); +*/ removeBlockAndDropAsItem(world, X + i, Y + k, Z + j); + } + } + } + } + + else if (FACING.equals("facingEast") || FACING.equals("facingWest")){ + + //Set Player Facing + Z = (int) aPlayer.posZ; + Y = (int) aPlayer.posY; + if (FACING.equals("facingEast")){ + X = (int) aPlayer.posX + 1;} + else { + X = (int) aPlayer.posX - 1;} + + + DURABILITY_LOSS = 0; + for(int i = -1; i < 2; i++) { + for(int j = -1; j < 2; j++) { + for(int k = -1; k < 2; k++) { + /*float dur = calculateDurabilityLoss(world, X+k, Y + i, Z + j); + DURABILITY_LOSS = (DURABILITY_LOSS + dur); + Utils.LOG_WARNING("Added Loss: "+dur);*/ + removeBlockAndDropAsItem(world, X+k, Y + i, Z + j); + } + } + } + } + + else if (FACING.equals("facingNorth") || FACING.equals("facingSouth")){ + + //Set Player Facing + X = (int) aPlayer.posX; + Y = (int) aPlayer.posY; + + if (FACING.equals("facingNorth")){ + Z = (int) aPlayer.posZ + 1;} + else { + Z = (int) aPlayer.posZ - 1;} + + DURABILITY_LOSS = 0; + for(int i = -1; i < 2; i++) { + for(int j = -1; j < 2; j++) { + for(int k = -1; k < 2; k++) { + /*float dur = calculateDurabilityLoss(world, X + j, Y + i, Z+k); + DURABILITY_LOSS = (DURABILITY_LOSS + dur); + Utils.LOG_WARNING("Added Loss: "+dur);*/ + removeBlockAndDropAsItem(world, X + j, Y + i, Z+k); + } + } + } + } + + //Set Durability damage to the item + if (durabilityDamage == true){ + Utils.LOG_WARNING("Total Loss: "+(int)DURABILITY_LOSS); + if (stack.getItemDamage() < (stack.getMaxDamage()-DURABILITY_LOSS)){ + stack.damageItem((int) DURABILITY_LOSS, aPlayer); + } + } + DURABILITY_LOSS = 0; + } + } + + + public static boolean getBlockType(Block block){ + final String LIQUID = "liquid"; + final String BLOCK = "block"; + final String ORE = "ore"; + final String AIR = "air"; + String blockClass = ""; + + try { + blockClass = block.getClass().toString().toLowerCase(); + Utils.LOG_WARNING(blockClass); + if (blockClass.toLowerCase().contains(LIQUID)){ + Utils.LOG_WARNING(block.toString()+" is a Liquid."); + return false; + } + else if (blockClass.toLowerCase().contains(ORE)){ + Utils.LOG_WARNING(block.toString()+" is an Ore."); + return true; + } + else if (blockClass.toLowerCase().contains(AIR)){ + Utils.LOG_WARNING(block.toString()+" is Air."); + return false; + } + else if (blockClass.toLowerCase().contains(BLOCK)){ + Utils.LOG_WARNING(block.toString()+" is a block of some kind."); + return false; + } + else { + Utils.LOG_WARNING(block.toString()+" is mystery."); + return false; + } + } + catch(NullPointerException e){ + return false; + } + } + + +} |