From 04444fc2985947396f316e596a5827a43500bf87 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Tue, 27 Aug 2019 16:26:35 +0200 Subject: [PATCH] This File gets a Refactoring, that File gets a Refactoring, everybody gets a Refactoring! --- .../Slimefun/Objects/Research.java | 5 +- .../SlimefunItem/abstractItems/AFarm.java | 97 +++++++------- .../machines/ReactorAccessPort.java | 75 +++++------ .../Slimefun/Objects/tasks/MagnetTask.java | 2 +- .../Slimefun/Setup/SlimefunSetup.java | 42 +++--- .../mrCookieSlime/Slimefun/SlimefunGuide.java | 5 +- .../androids/ProgrammableAndroid.java | 25 ++-- .../Slimefun/api/BlockStorage.java | 18 +-- .../Slimefun/api/ErrorReport.java | 7 +- .../Slimefun/api/PlayerProfile.java | 2 +- .../Slimefun/api/SlimefunBackup.java | 123 +++++++++--------- .../Slimefun/api/network/Network.java | 40 +++--- .../hooks/github/GitHubConnector.java | 56 ++++---- .../Slimefun/hooks/github/IntegerFormat.java | 8 +- 14 files changed, 256 insertions(+), 249 deletions(-) diff --git a/src/me/mrCookieSlime/Slimefun/Objects/Research.java b/src/me/mrCookieSlime/Slimefun/Objects/Research.java index 80ec340bb..a9d522c73 100644 --- a/src/me/mrCookieSlime/Slimefun/Objects/Research.java +++ b/src/me/mrCookieSlime/Slimefun/Objects/Research.java @@ -1,6 +1,7 @@ package me.mrCookieSlime.Slimefun.Objects; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -363,7 +364,7 @@ public class Research { public static void sendStats(CommandSender sender, Player p) { PlayerProfile profile = PlayerProfile.fromUUID(p.getUniqueId()); Set researched = profile.getResearches(); - int levels = researched.stream().mapToInt(r -> r.getCost()).sum(); + int levels = researched.stream().mapToInt(Research::getCost).sum(); String progress = String.valueOf(Math.round(((researched.size() * 100.0F) / list().size()) * 100.0F) / 100.0F); if (Float.parseFloat(progress) < 16.0F) progress = "&4" + progress + " &r% "; @@ -392,7 +393,7 @@ public class Research { * @see #sendStats(CommandSender, Player) */ @Deprecated - public static String getTitle(Player p, Set researched) { + public static String getTitle(Player p, Collection researched) { int index = Math.round(Float.valueOf(String.valueOf(Math.round(((researched.size() * 100.0F) / list().size()) * 100.0F) / 100.0F)) / 100.0F) * titles.size(); if (index > 0) index--; return titles.get(index); diff --git a/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AFarm.java b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AFarm.java index e66b2f00e..190827a0e 100644 --- a/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AFarm.java +++ b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AFarm.java @@ -27,15 +27,15 @@ import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; public abstract class AFarm extends SlimefunItem { - + private static final int[] border = {0, 1, 2, 3, 4, 5, 6, 7, 8, 36, 37, 38, 39, 40, 41, 42, 43, 44}; private static final int[] border_out = {9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}; public AFarm(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe) { super(category, item, id, recipeType, recipe); - + new BlockMenuPreset(id, getInventoryTitle()) { - + @Override public void init() { constructMenu(this); @@ -43,6 +43,7 @@ public abstract class AFarm extends SlimefunItem { @Override public void newInstance(BlockMenu menu, Block b) { + } @Override @@ -56,13 +57,13 @@ public abstract class AFarm extends SlimefunItem { return new int[0]; } }; - + registerBlockHandler(id, new SlimefunBlockHandler() { - + @Override public void onPlace(Player p, Block b, SlimefunItem item) { } - + @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { BlockMenu inv = BlockStorage.getInventory(b); @@ -81,9 +82,9 @@ public abstract class AFarm extends SlimefunItem { public AFarm(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) { super(category, item, id, recipeType, recipe, recipeOutput); - + new BlockMenuPreset(id, getInventoryTitle()) { - + @Override public void init() { constructMenu(this); @@ -104,13 +105,13 @@ public abstract class AFarm extends SlimefunItem { return new int[0]; } }; - + registerBlockHandler(id, new SlimefunBlockHandler() { - + @Override public void onPlace(Player p, Block b, SlimefunItem item) { } - + @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { BlockMenu inv = BlockStorage.getInventory(b); @@ -126,26 +127,21 @@ public abstract class AFarm extends SlimefunItem { } }); } - + private void constructMenu(BlockMenuPreset preset) { for (int i : border) { - preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } + for (int i : border_out) { - preset.addItem(i, new CustomItem(new ItemStack(Material.ORANGE_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.ORANGE_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } - - preset.addItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); - + + preset.addItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); + for (int i : getOutputSlots()) { preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() { - + @Override public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) { return false; @@ -158,41 +154,46 @@ public abstract class AFarm extends SlimefunItem { }); } } - + public abstract String getInventoryTitle(); public abstract int getEnergyConsumption(); public abstract boolean canHarvest(Block b); public abstract ItemStack harvest(Block b); public abstract int getSize(); - + public int[] getOutputSlots() { return new int[] {19, 20, 21, 22, 23, 24, 25}; } - + protected void tick(Block b) { if (ChargableBlock.isChargable(b)) { if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return; int i = getSize() / 2; - loop: - for (int x = -i; x <= i; x++) { - for (int z = -i; z <= i; z++) { - Block block = new Location(b.getWorld(), b.getX() + x, b.getY() + 2, b.getZ() + z).getBlock(); - if (canHarvest(block)) { - ItemStack item = harvest(block); - if (!fits(block, new ItemStack[] {item})) break loop; - pushItems(b, new ItemStack[] {item}); - ChargableBlock.addCharge(b, -getEnergyConsumption()); - break loop; - } - } - } + + for (int x = -i; x <= i; x++) { + for (int z = -i; z <= i; z++) { + Block block = new Location(b.getWorld(), b.getX() + (double) x, b.getY() + 2.0, b.getZ() + (double) z).getBlock(); + if (canHarvest(block)) { + ItemStack item = harvest(block); + + if (!fits(block, item)) { + return; + } + + pushItems(b, item); + ChargableBlock.addCharge(b, -getEnergyConsumption()); + + return; + } + } + } } } - + @Override public void register(boolean slimefun) { addItemHandler(new BlockTicker() { - + @Override public void tick(Block b, SlimefunItem sf, Config data) { AFarm.this.tick(b); @@ -210,7 +211,7 @@ public abstract class AFarm extends SlimefunItem { super.register(slimefun); } - + private Inventory inject(Block b) { int size = BlockStorage.getInventory(b).toInventory().getSize(); Inventory inv = Bukkit.createInventory(null, size); @@ -222,15 +223,15 @@ public abstract class AFarm extends SlimefunItem { } return inv; } - - protected boolean fits(Block b, ItemStack[] items) { + + protected boolean fits(Block b, ItemStack... items) { return inject(b).addItem(items).isEmpty(); } - - protected void pushItems(Block b, ItemStack[] items) { + + protected void pushItems(Block b, ItemStack... items) { Inventory inv = inject(b); inv.addItem(items); - + for (int slot: getOutputSlots()) { BlockStorage.getInventory(b).replaceExistingItem(slot, inv.getItem(slot)); } diff --git a/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/ReactorAccessPort.java b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/ReactorAccessPort.java index 9158a2c1a..5cd2d8761 100644 --- a/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/ReactorAccessPort.java +++ b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/ReactorAccessPort.java @@ -44,24 +44,29 @@ public class ReactorAccessPort extends SlimefunItem { @Override public void newInstance(BlockMenu menu, Block b) { + } @Override public boolean canOpen(Block b, Player p) { if(p.hasPermission("slimefun.inventory.bypass") || CSCoreLib.getLib().getProtectionManager().canAccessChest(p.getUniqueId(),b,true)) { - - AReactor reactor = getReactor(b.getLocation()); - if(reactor != null) { + if (reactor != null) { boolean empty = true; BlockMenu bm = getReactorMenu(b.getLocation()); - if(bm != null) { - for(int slot:reactor.getCoolantSlots()) - if(bm.getItemInSlot(slot) != null) + + if (bm != null) { + for (int slot: reactor.getCoolantSlots()) { + if (bm.getItemInSlot(slot) != null) { empty = false; - for(int slot:reactor.getFuelSlots()) - if(bm.getItemInSlot(slot) != null) + } + } + + for (int slot: reactor.getFuelSlots()) { + if (bm.getItemInSlot(slot) != null) { empty = false; + } + } if(!empty || !p.isSneaking()) { //reactor is not empty, lets view it's inventory instead. @@ -100,11 +105,13 @@ public class ReactorAccessPort extends SlimefunItem { @Override public void onPlace(Player p, Block b, SlimefunItem item) { + } @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { BlockMenu inv = BlockStorage.getInventory(b); + if (inv != null) { for (int slot : getFuelSlots()) { if (inv.getItemInSlot(slot) != null) { @@ -112,12 +119,14 @@ public class ReactorAccessPort extends SlimefunItem { inv.replaceExistingItem(slot, null); } } + for (int slot : getCoolantSlots()) { if (inv.getItemInSlot(slot) != null) { b.getWorld().dropItemNaturally(b.getLocation(), inv.getItemInSlot(slot)); inv.replaceExistingItem(slot, null); } } + for (int slot : getOutputSlots()) { if (inv.getItemInSlot(slot) != null) { b.getWorld().dropItemNaturally(b.getLocation(), inv.getItemInSlot(slot)); @@ -132,44 +141,24 @@ public class ReactorAccessPort extends SlimefunItem { private void constructMenu(BlockMenuPreset preset) { for (int i : border) { - preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } for (int i : border_1) { - preset.addItem(i, new CustomItem(new ItemStack(Material.LIME_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.LIME_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } for (int i : border_2) { - preset.addItem(i, new CustomItem(new ItemStack(Material.CYAN_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.CYAN_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } for (int i : border_3) { - preset.addItem(i, new CustomItem(new ItemStack(Material.GREEN_STAINED_GLASS_PANE), " "), - (p, slot, item, action) -> false - ); + preset.addItem(i, new CustomItem(new ItemStack(Material.GREEN_STAINED_GLASS_PANE), " "), (p, slot, item, action) -> false); } - preset.addItem(1, new CustomItem(SlimefunItems.URANIUM, "&7Fuel Slot", "", "&rThis Slot accepts radioactive Fuel such as:", "&2Uranium &ror &aNeptunium"), - (p, slot, item, action) -> false - ); - - preset.addItem(22, new CustomItem(SlimefunItems.PLUTONIUM, "&7Byproduct Slot", "", "&rThis Slot contains the Reactor's Byproduct", "&rsuch as &aNeptunium &ror &7Plutonium"), - (p, slot, item, action) -> false - ); - - preset.addItem(7, new CustomItem(SlimefunItems.REACTOR_COOLANT_CELL, "&bCoolant Slot", "", "&rThis Slot accepts Coolant Cells", "&4Without any Coolant Cells, your Reactor", "&4will explode"), - (p, slot, item, action) -> false - ); - - preset.addItem(7, new CustomItem(SlimefunItems.REACTOR_COOLANT_CELL, "&bCoolant Slot", "", "&rThis Slot accepts Coolant Cells", "&4Without any Coolant Cells, your Reactor", "&4will explode"), - (p, slot, item, action) -> false - ); + preset.addItem(1, new CustomItem(SlimefunItems.URANIUM, "&7Fuel Slot", "", "&rThis Slot accepts radioactive Fuel such as:", "&2Uranium &ror &aNeptunium"), (p, slot, item, action) -> false); + preset.addItem(22, new CustomItem(SlimefunItems.PLUTONIUM, "&7Byproduct Slot", "", "&rThis Slot contains the Reactor's Byproduct", "&rsuch as &aNeptunium &ror &7Plutonium"), (p, slot, item, action) -> false); + preset.addItem(7, new CustomItem(SlimefunItems.REACTOR_COOLANT_CELL, "&bCoolant Slot", "", "&rThis Slot accepts Coolant Cells", "&4Without any Coolant Cells, your Reactor", "&4will explode"),(p, slot, item, action) -> false); } public String getInventoryTitle() { @@ -196,8 +185,7 @@ public class ReactorAccessPort extends SlimefunItem { Location reactorL = new Location(l.getWorld(), l.getX(), l.getY() - 3, l.getZ()); SlimefunItem item = BlockStorage.check(reactorL.getBlock()); - if(item instanceof AReactor) - return (AReactor) item; + if (item instanceof AReactor) return (AReactor) item; return null; } @@ -207,8 +195,9 @@ public class ReactorAccessPort extends SlimefunItem { String id = BlockStorage.checkID(reactorL); - if(id.equals("NUCLEAR_REACTOR") || id.equals("NETHERSTAR_REACTOR")) + if(id.equals("NUCLEAR_REACTOR") || id.equals("NETHERSTAR_REACTOR")) { return BlockStorage.getInventory(reactorL); + } return null; } @@ -216,12 +205,15 @@ public class ReactorAccessPort extends SlimefunItem { private static Inventory inject(Location l) { int size = BlockStorage.getInventory(l).toInventory().getSize(); Inventory inv = Bukkit.createInventory(null, size); + for (int i = 0; i < size; i++) { inv.setItem(i, new CustomItem(Material.COMMAND_BLOCK, " &4ALL YOUR PLACEHOLDERS ARE BELONG TO US")); } + for (int slot : getOutputSlots()) { inv.setItem(slot, BlockStorage.getInventory(l).getItemInSlot(slot)); } + return inv; } @@ -232,11 +224,8 @@ public class ReactorAccessPort extends SlimefunItem { for (int slot: getOutputSlots()) { BlockStorage.getInventory(l).replaceExistingItem(slot, inv.getItem(slot)); } - - for (Map.Entry entry: map.entrySet()) { - return entry.getValue(); - } - return null; + + return map.values().stream().findAny().orElse(null); } } diff --git a/src/me/mrCookieSlime/Slimefun/Objects/tasks/MagnetTask.java b/src/me/mrCookieSlime/Slimefun/Objects/tasks/MagnetTask.java index ecf026daf..39570eb02 100644 --- a/src/me/mrCookieSlime/Slimefun/Objects/tasks/MagnetTask.java +++ b/src/me/mrCookieSlime/Slimefun/Objects/tasks/MagnetTask.java @@ -27,7 +27,7 @@ public class MagnetTask extends SlimefunTask { @Override protected boolean cancelTask() { - return super.cancelTask() ? true: p.getGameMode() == GameMode.SPECTATOR; + return super.cancelTask() || p.getGameMode() == GameMode.SPECTATOR; } } diff --git a/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java b/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java index b4e85ca60..8d0fe8a1b 100644 --- a/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java +++ b/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java @@ -720,6 +720,8 @@ public class SlimefunSetup { new ItemStack[] {SlimefunItems.IRON_DUST, new ItemStack(Material.IRON_INGOT)}, Material.NETHER_BRICK_FENCE, new String[] {"chance.fireBreak"}, new Integer[] {34}) .register(true, new MultiBlockInteractionHandler() { + + private final String chamberID = "IGNITION_CHAMBER"; @Override public boolean onInteract(Player p, MultiBlock mb, Block b) { @@ -764,13 +766,17 @@ public class SlimefunSetup { // The chamber methods have been updated to reflect this change. // Maybe this code snippet should be turned into a loop? Hopper chamber = null; - if (BlockStorage.check(dispBlock.getRelative(BlockFace.EAST).getState().getBlock(), "IGNITION_CHAMBER")) { + + if (BlockStorage.check(dispBlock.getRelative(BlockFace.EAST).getState().getBlock(), chamberID)) { chamber = (Hopper) dispBlock.getRelative(BlockFace.EAST).getState(); - } else if (BlockStorage.check(dispBlock.getRelative(BlockFace.WEST).getState().getBlock(), "IGNITION_CHAMBER")) { + } + else if (BlockStorage.check(dispBlock.getRelative(BlockFace.WEST).getState().getBlock(), chamberID)) { chamber = (Hopper) dispBlock.getRelative(BlockFace.WEST).getState(); - } else if (BlockStorage.check(dispBlock.getRelative(BlockFace.NORTH).getState().getBlock(), "IGNITION_CHAMBER")) { + } + else if (BlockStorage.check(dispBlock.getRelative(BlockFace.NORTH).getState().getBlock(), chamberID)) { chamber = (Hopper) dispBlock.getRelative(BlockFace.NORTH).getState(); - } else if (BlockStorage.check(dispBlock.getRelative(BlockFace.SOUTH).getState().getBlock(), "IGNITION_CHAMBER")){ + } + else if (BlockStorage.check(dispBlock.getRelative(BlockFace.SOUTH).getState().getBlock(), chamberID)){ chamber = (Hopper) dispBlock.getRelative(BlockFace.SOUTH).getState(); } @@ -1840,17 +1846,23 @@ public class SlimefunSetup { @Override public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List drops) { if (SlimefunManager.isItemSimiliar(item, SlimefunItems.PICKAXE_OF_CONTAINMENT, true)) { - Block b = e.getBlock(); // Refactored it into this so we don't need to call e.getBlock() all the time. + // Refactored it into this so we don't need to call e.getBlock() all the time. + Block b = e.getBlock(); if (b.getType() != Material.SPAWNER) return true; + // If the spawner's BlockStorage has BlockInfo, then it's not a vanilla spawner and shouldn't give a broken spawner. ItemStack spawner = SlimefunItems.BROKEN_SPAWNER.clone(); - if(BlockStorage.hasBlockInfo(b)) + if (BlockStorage.hasBlockInfo(b)) { spawner = SlimefunItems.REPAIRED_SPAWNER.clone(); + } + ItemMeta im = spawner.getItemMeta(); List lore = im.getLore(); + for (int i = 0; i < lore.size(); i++) { if (lore.get(i).contains("")) lore.set(i, lore.get(i).replace("", StringUtils.format(((CreatureSpawner) b.getState()).getSpawnedType().toString()))); } + im.setLore(lore); spawner.setItemMeta(im); b.getLocation().getWorld().dropItemNaturally(b.getLocation(), spawner); @@ -3860,11 +3872,11 @@ public class SlimefunSetup { public void tick(Block b, SlimefunItem item, Config data) { int charge = ChargableBlock.getCharge(b.getLocation()); if (charge > 2) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4.0 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); ChargableBlock.setCharge(b.getLocation(), charge - 3); } else { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4.0 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); } } @@ -3887,7 +3899,7 @@ public class SlimefunSetup { @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 4.0 + 100, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); return true; } }); @@ -3900,11 +3912,11 @@ public class SlimefunSetup { public void tick(Block b, SlimefunItem item, Config data) { int charge = ChargableBlock.getCharge(b.getLocation()); if (charge > 10) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16.0 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); ChargableBlock.setCharge(b.getLocation(), charge - 11); } else { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16.0 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); } } @@ -3927,7 +3939,7 @@ public class SlimefunSetup { @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 16.0 + 500, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); return true; } }); @@ -3940,11 +3952,11 @@ public class SlimefunSetup { public void tick(Block b, SlimefunItem item, Config data) { int charge = ChargableBlock.getCharge(b.getLocation()); if (charge > 45) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64.0 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.ONLINE); ChargableBlock.setCharge(b.getLocation(), charge - 46); } else { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64.0 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); } } @@ -3967,7 +3979,7 @@ public class SlimefunSetup { @Override public boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason) { - Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); + Slimefun.getGPSNetwork().updateTransmitter(new Location(b.getWorld(), b.getX(), b.getY() * 64.0 + 800, b.getZ()), UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), NetworkStatus.OFFLINE); return true; } }); diff --git a/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java b/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java index 479d46b5d..e184d7a5e 100644 --- a/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java +++ b/src/me/mrCookieSlime/Slimefun/SlimefunGuide.java @@ -95,6 +95,7 @@ public class SlimefunGuide { return getItem(book ? BookDesign.BOOK: BookDesign.CHEST); } + @Deprecated public static ItemStack getDeprecatedItem(boolean book) { return new CustomItem(new ItemStack(Material.ENCHANTED_BOOK), "&eSlimefun Guide &7(Right Click)", (book ? "": "&2"), "&rThis is your basic Guide for Slimefun", "&rYou can see all Items added by this Plugin", "&ror its Addons including their Recipes", "&ra bit of information and more"); } @@ -1018,7 +1019,7 @@ public class SlimefunGuide { List lore = new ArrayList<>(); lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &7Lasts " + getTimeLeft(fuel.getTicks() / 2))); lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + (((AGenerator) sfItem).getEnergyProduction() * 2) + " J/s")); - lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + DoubleHandler.getFancyDouble(fuel.getTicks() * ((AGenerator) sfItem).getEnergyProduction()) + " J in total")); + lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + DoubleHandler.getFancyDouble((double) fuel.getTicks() * ((AGenerator) sfItem).getEnergyProduction()) + " J in total")); im.setLore(lore); fItem.setItemMeta(im); menu.addItem(slot, fItem); @@ -1037,7 +1038,7 @@ public class SlimefunGuide { List lore = new ArrayList<>(); lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &7Lasts " + getTimeLeft(fuel.getTicks() / 2))); lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + (((AReactor) sfItem).getEnergyProduction() * 2) + " J/s")); - lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + DoubleHandler.getFancyDouble(fuel.getTicks() * ((AReactor) sfItem).getEnergyProduction()) + " J in total")); + lore.add(ChatColor.translateAlternateColorCodes('&', "&8\u21E8 &e\u26A1 &7" + DoubleHandler.getFancyDouble((double) fuel.getTicks() * ((AReactor) sfItem).getEnergyProduction()) + " J in total")); im.setLore(lore); fItem.setItemMeta(im); menu.addItem(slot, fItem); diff --git a/src/me/mrCookieSlime/Slimefun/androids/ProgrammableAndroid.java b/src/me/mrCookieSlime/Slimefun/androids/ProgrammableAndroid.java index a3495465f..39a5ac4f1 100644 --- a/src/me/mrCookieSlime/Slimefun/androids/ProgrammableAndroid.java +++ b/src/me/mrCookieSlime/Slimefun/androids/ProgrammableAndroid.java @@ -713,19 +713,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem { if (!blockblacklist.contains(block.getType()) && !drops.isEmpty() && CSCoreLib.getLib().getProtectionManager().canBuild(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner")), block)) { SlimefunItem item = BlockStorage.check(block); - if (item != null) { - return; - /*if (fits(b, item.getItem())) { - if (SlimefunItem.blockhandler.containsKey(item.getID())) { - if (SlimefunItem.blockhandler.get(item.getID()).onBreak(null, block, item, UnregisterReason.ANDROID_DIG)) { - pushItems(b, BlockStorage.retrieve(block)); - if (SlimefunItem.blockhandler.containsKey(item.getID())) SlimefunItem.blockhandler.get(item.getID()).onBreak(null, block, item, UnregisterReason.ANDROID_DIG); - block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); - block.setType(Material.AIR); - } - } - }*/ - } else { + if (item == null) { ItemStack[] items = drops.toArray(new ItemStack[drops.size()]); if (fits(b, items)) { pushItems(b, items); @@ -733,6 +721,17 @@ public abstract class ProgrammableAndroid extends SlimefunItem { block.setType(Material.AIR); } } + /* + else if (fits(b, item.getItem())) { + if (SlimefunItem.blockhandler.containsKey(item.getID())) { + if (SlimefunItem.blockhandler.get(item.getID()).onBreak(null, block, item, UnregisterReason.ANDROID_DIG)) { + pushItems(b, BlockStorage.retrieve(block)); + if (SlimefunItem.blockhandler.containsKey(item.getID())) SlimefunItem.blockhandler.get(item.getID()).onBreak(null, block, item, UnregisterReason.ANDROID_DIG); + block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); + block.setType(Material.AIR); + } + } + }*/ } } diff --git a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java index 7b91d54a8..de9aea985 100644 --- a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -609,16 +609,18 @@ public class BlockStorage { public void clearInventory(Location l) { BlockMenu menu = getInventory(l); + + if (menu != null) { + for (HumanEntity human : new ArrayList<>(menu.toInventory().getViewers())) { + // Prevents "java.lang.IllegalStateException: Asynchronous entity add!" when closing inventory while holding an item + Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunStartup.instance, () -> { + human.closeInventory(); + }); + } - for (HumanEntity human : new ArrayList<>(menu.toInventory().getViewers())) { - // Prevents "java.lang.IllegalStateException: Asynchronous entity add!" when closing inventory while holding an item - Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunStartup.instance, () -> { - human.closeInventory(); - }); + inventories.get(l).delete(l); + inventories.remove(l); } - - inventories.get(l).delete(l); - inventories.remove(l); } public boolean hasInventory(Location l) { diff --git a/src/me/mrCookieSlime/Slimefun/api/ErrorReport.java b/src/me/mrCookieSlime/Slimefun/api/ErrorReport.java index 3572a5a10..3624c00be 100644 --- a/src/me/mrCookieSlime/Slimefun/api/ErrorReport.java +++ b/src/me/mrCookieSlime/Slimefun/api/ErrorReport.java @@ -24,13 +24,14 @@ public class ErrorReport { private File file; public ErrorReport(Throwable throwable, Consumer printer) { - file = new File("plugins/Slimefun/error-reports/" + Clock.getFormattedTime() + ".err"); + String path = "plugins/Slimefun/error-reports/" + Clock.getFormattedTime(); + file = new File(path + ".err"); if (file.exists()) { - IntStream stream = IntStream.iterate(1, i -> i + 1).filter(i -> !new File("plugins/Slimefun/error-reports/" + Clock.getFormattedTime() + "(" + i + ").err").exists()); + IntStream stream = IntStream.iterate(1, i -> i + 1).filter(i -> !new File(path + " (" + i + ").err").exists()); int id = stream.findFirst().getAsInt(); - file = new File("plugins/Slimefun/error-reports/" + Clock.getFormattedTime() + "(" + id + ").err"); + file = new File(path + " (" + id + ").err"); } try (PrintStream stream = new PrintStream(file)) { diff --git a/src/me/mrCookieSlime/Slimefun/api/PlayerProfile.java b/src/me/mrCookieSlime/Slimefun/api/PlayerProfile.java index f22f0a9c3..ee058a098 100644 --- a/src/me/mrCookieSlime/Slimefun/api/PlayerProfile.java +++ b/src/me/mrCookieSlime/Slimefun/api/PlayerProfile.java @@ -23,7 +23,7 @@ import me.mrCookieSlime.Slimefun.api.inventory.BackpackInventory; * @author TheBusyBiscuit * */ -public class PlayerProfile { +public final class PlayerProfile { public static Map profiles = new HashMap<>(); diff --git a/src/me/mrCookieSlime/Slimefun/api/SlimefunBackup.java b/src/me/mrCookieSlime/Slimefun/api/SlimefunBackup.java index 170197ce6..c8da97b26 100644 --- a/src/me/mrCookieSlime/Slimefun/api/SlimefunBackup.java +++ b/src/me/mrCookieSlime/Slimefun/api/SlimefunBackup.java @@ -8,7 +8,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; @@ -17,20 +16,17 @@ import java.util.zip.ZipOutputStream; import me.mrCookieSlime.CSCoreLibPlugin.general.Clock; public class SlimefunBackup { - + public static void start() { File folder = new File("data-storage/Slimefun/block-backups"); List backups = Arrays.asList(folder.listFiles()); - if (backups.size() > 20) { - Collections.sort(backups, new Comparator() { - @Override - public int compare(File f1, File f2) { - try { - return (int) (new SimpleDateFormat("yyyy-MM-dd-HH-mm").parse(f1.getName().replace(".zip", "")).getTime() - new SimpleDateFormat("yyyy-MM-dd-HH-mm").parse(f2.getName().replace(".zip", "")).getTime()); - } catch (ParseException e) { - return 0; - } + if (backups.size() > 20) { + Collections.sort(backups, (a, b) -> { + try { + return (int) (new SimpleDateFormat("yyyy-MM-dd-HH-mm").parse(a.getName().replace(".zip", "")).getTime() - new SimpleDateFormat("yyyy-MM-dd-HH-mm").parse(b.getName().replace(".zip", "")).getTime()); + } catch (ParseException e) { + return 0; } }); @@ -49,67 +45,68 @@ public class SlimefunBackup { try { file.createNewFile(); - ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file)); + try (ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file))) { + for (File f1: new File("data-storage/Slimefun/stored-blocks/").listFiles()) { + for (File f: f1.listFiles()) { + ZipEntry entry = new ZipEntry("stored-blocks/" + f1.getName() + "/" + f.getName()); + output.putNextEntry(entry); - for (File f1: new File("data-storage/Slimefun/stored-blocks/").listFiles()) { - for (File f: f1.listFiles()) { - ZipEntry entry = new ZipEntry("stored-blocks/" + f1.getName() + "/" + f.getName()); + try (FileInputStream input = new FileInputStream(f)) { + int length; + while ((length = input.read(buffer)) > 0) { + output.write(buffer, 0, length); + } + } + + output.closeEntry(); + } + } + + for (File f: new File("data-storage/Slimefun/universal-inventories/").listFiles()) { + ZipEntry entry = new ZipEntry("universal-inventories/" + f.getName()); output.putNextEntry(entry); - FileInputStream input = new FileInputStream(f); - int length; - while ((length = input.read(buffer)) > 0) { - output.write(buffer, 0, length); + try (FileInputStream input = new FileInputStream(f)) { + int length; + while ((length = input.read(buffer)) > 0) { + output.write(buffer, 0, length); + } + } + + output.closeEntry(); + } + + for (File f: new File("data-storage/Slimefun/stored-inventories/").listFiles()) { + ZipEntry entry = new ZipEntry("stored-inventories/" + f.getName()); + output.putNextEntry(entry); + + try (FileInputStream input = new FileInputStream(f)) { + int length; + while ((length = input.read(buffer)) > 0) { + output.write(buffer, 0, length); + } + } + + output.closeEntry(); + } + + File chunks = new File("data-storage/Slimefun/stored-chunks/chunks.sfc"); + + if (chunks.exists()) { + ZipEntry entry = new ZipEntry("stored-chunks/chunks.sfc"); + output.putNextEntry(entry); + + try (FileInputStream input = new FileInputStream(chunks)) { + int length; + while ((length = input.read(buffer)) > 0) { + output.write(buffer, 0, length); + } } - input.close(); output.closeEntry(); } } - for (File f: new File("data-storage/Slimefun/universal-inventories/").listFiles()) { - ZipEntry entry = new ZipEntry("universal-inventories/" + f.getName()); - output.putNextEntry(entry); - FileInputStream input = new FileInputStream(f); - - int length; - while ((length = input.read(buffer)) > 0) { - output.write(buffer, 0, length); - } - - input.close(); - output.closeEntry(); - } - - for (File f: new File("data-storage/Slimefun/stored-inventories/").listFiles()) { - ZipEntry entry = new ZipEntry("stored-inventories/" + f.getName()); - output.putNextEntry(entry); - FileInputStream input = new FileInputStream(f); - - int length; - while ((length = input.read(buffer)) > 0) { - output.write(buffer, 0, length); - } - - input.close(); - output.closeEntry(); - } - - if (new File("data-storage/Slimefun/stored-chunks/chunks.sfc").exists()) { - ZipEntry entry = new ZipEntry("stored-chunks/chunks.sfc"); - output.putNextEntry(entry); - FileInputStream input = new FileInputStream(new File("data-storage/Slimefun/stored-chunks/chunks.sfc")); - - int length; - while ((length = input.read(buffer)) > 0) { - output.write(buffer, 0, length); - } - - input.close(); - output.closeEntry(); - } - - output.close(); System.out.println("[Slimefun] Backed up Blocks to " + file.getName()); } catch(IOException e) { e.printStackTrace(); diff --git a/src/me/mrCookieSlime/Slimefun/api/network/Network.java b/src/me/mrCookieSlime/Slimefun/api/network/Network.java index cb192e3e2..b3a84cd24 100644 --- a/src/me/mrCookieSlime/Slimefun/api/network/Network.java +++ b/src/me/mrCookieSlime/Slimefun/api/network/Network.java @@ -97,9 +97,11 @@ public abstract class Network { private Component getCurrentClassification(Location l) { if(regulatorNodes.contains(l)) { return Component.REGULATOR; - } else if(connectorNodes.contains(l)) { + } + else if(connectorNodes.contains(l)) { return Component.CONNECTOR; - } else if(terminusNodes.contains(l)) { + } + else if(terminusNodes.contains(l)) { return Component.TERMINUS; } return null; @@ -107,27 +109,33 @@ public abstract class Network { private void discoverStep() { int steps = 0; - while(nodeQueue.peek() != null) { + while (nodeQueue.peek() != null) { Location l = nodeQueue.poll(); Component currentAssignment = getCurrentClassification(l); Component classification = classifyLocation(l); - if(classification != currentAssignment) { - if(currentAssignment == Component.REGULATOR || currentAssignment == Component.CONNECTOR) { + + if (classification != currentAssignment) { + if (currentAssignment == Component.REGULATOR || currentAssignment == Component.CONNECTOR) { // Requires a complete rebuild of the network, so we just throw the current one away. unregisterNetwork(this); return; - } else if(currentAssignment == Component.TERMINUS) { + } + else if (currentAssignment == Component.TERMINUS) { terminusNodes.remove(l); } - if(classification == Component.REGULATOR) { + + if (classification == Component.REGULATOR) { regulatorNodes.add(l); discoverNeighbors(l); - } else if(classification == Component.CONNECTOR) { + } + else if(classification == Component.CONNECTOR) { connectorNodes.add(l); discoverNeighbors(l); - } else if(classification == Component.TERMINUS) { + } + else if(classification == Component.TERMINUS) { terminusNodes.add(l); } + locationClassificationChange(l, currentAssignment, classification); } steps += 1; @@ -137,7 +145,7 @@ public abstract class Network { } } - private void discoverNeighbors(Location l, int xDiff, int yDiff, int zDiff) { + private void discoverNeighbors(Location l, double xDiff, double yDiff, double zDiff) { for(int i = getRange() + 1; i > 0; i --) { Location new_location = l.clone().add(i * xDiff, i * yDiff, i * zDiff); addLocationToNetwork(new_location); @@ -145,12 +153,12 @@ public abstract class Network { } private void discoverNeighbors(Location l) { - discoverNeighbors(l, 1, 0, 0); - discoverNeighbors(l, -1, 0, 0); - discoverNeighbors(l, 0, 1, 0); - discoverNeighbors(l, 0, -1, 0); - discoverNeighbors(l, 0, 0, 1); - discoverNeighbors(l, 0, 0, -1); + discoverNeighbors(l, 1.0, 0.0, 0.0); + discoverNeighbors(l, -1.0, 0.0, 0.0); + discoverNeighbors(l, 0.0, 1.0, 0.0); + discoverNeighbors(l, 0.0, -1.0, 0.0); + discoverNeighbors(l, 0.0, 0.0, 1.0); + discoverNeighbors(l, 0.0, 0.0, -1.0); } public void display() { diff --git a/src/me/mrCookieSlime/Slimefun/hooks/github/GitHubConnector.java b/src/me/mrCookieSlime/Slimefun/hooks/github/GitHubConnector.java index d43233aab..0cc5f1bee 100644 --- a/src/me/mrCookieSlime/Slimefun/hooks/github/GitHubConnector.java +++ b/src/me/mrCookieSlime/Slimefun/hooks/github/GitHubConnector.java @@ -16,39 +16,40 @@ import com.google.gson.JsonParser; import me.mrCookieSlime.Slimefun.SlimefunStartup; public abstract class GitHubConnector { - + private File file; public GitHubConnector() { this.file = new File("plugins/Slimefun/cache/github/" + this.getFileName() + ".json"); SlimefunStartup.instance.getUtilities().connectors.add(this); } - + public abstract String getFileName(); public abstract String getRepository(); public abstract String getURLSuffix(); public abstract void onSuccess(JsonElement element); public abstract void onFailure(); - + public void pullFile() { if (SlimefunStartup.getCfg().getBoolean("options.print-out-github-data-retrieving")) System.out.println("[Slimefun - GitHub] Retrieving '" + this.getFileName() + ".json' from GitHub..."); - + try { URL website = new URL("https://api.github.com/repos/" + this.getRepository() + this.getURLSuffix()); - + URLConnection connection = website.openConnection(); - connection.setConnectTimeout(3000); - connection.addRequestProperty("User-Agent", "Slimefun 4 GitHub Agent (by TheBusyBiscuit)"); - connection.setDoOutput(true); - - ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream()); - FileOutputStream fos = new FileOutputStream(file); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - this.parseData(); + connection.setConnectTimeout(3000); + connection.addRequestProperty("User-Agent", "Slimefun 4 GitHub Agent (by TheBusyBiscuit)"); + connection.setDoOutput(true); + + try (ReadableByteChannel channel = Channels.newChannel(connection.getInputStream())) { + try (FileOutputStream stream = new FileOutputStream(file)) { + stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE); + this.parseData(); + } + } } catch (IOException e) { if (SlimefunStartup.getCfg().getBoolean("options.print-out-github-data-retrieving")) System.err.println("[Slimefun - GitHub] ERROR - Could not connect to GitHub in time."); - + if (hasData()) { this.parseData(); } @@ -57,31 +58,26 @@ public abstract class GitHubConnector { } } } - + public boolean hasData() { return this.getFile().exists(); } - + public File getFile() { return this.file; } public void parseData() { - try { - BufferedReader reader = new BufferedReader(new FileReader(this.getFile())); - + try (BufferedReader reader = new BufferedReader(new FileReader(this.getFile()))) { String full = ""; - + String line; - while ((line = reader.readLine()) != null) { - full = full + line; - } - - reader.close(); - - JsonElement element = new JsonParser().parse(full); - - this.onSuccess(element); + while ((line = reader.readLine()) != null) { + full = full + line; + } + + JsonElement element = new JsonParser().parse(full); + this.onSuccess(element); } catch (IOException e) { e.printStackTrace(); diff --git a/src/me/mrCookieSlime/Slimefun/hooks/github/IntegerFormat.java b/src/me/mrCookieSlime/Slimefun/hooks/github/IntegerFormat.java index 3b4d1cf82..0579716af 100644 --- a/src/me/mrCookieSlime/Slimefun/hooks/github/IntegerFormat.java +++ b/src/me/mrCookieSlime/Slimefun/hooks/github/IntegerFormat.java @@ -6,17 +6,17 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -public class IntegerFormat { +public final class IntegerFormat { + + private IntegerFormat() {} - private static final SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public static String formatBigNumber(int i) { return NumberFormat.getNumberInstance(Locale.US).format(i); } public static Date parseGitHubDate(String str) { try { - return date_format.parse(str.replace("T", " ").replace("Z", "")); + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str.replace("T", " ").replace("Z", "")); } catch (ParseException e) { e.printStackTrace(); return null;