diff --git a/changelog.txt b/changelog.txt index f2a94a1..023cb00 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,6 @@ +1.0.37 + Added full 1.13 support + 1.0.36 Added support for transparent images. You can now load item icons without having to break the blocks for the transparent pixels Fixed blocks for custom resourcepacks diff --git a/plugin.yml b/plugin.yml index c87ca86..d2f50b0 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,7 @@ main: me.zombie_striker.pixelprinter.PixelPrinter -version: 1.0.36 +version: 1.0.37 name: PixelPrinter +api-version: 1.13 commands: PixelPrinter: description: Create images using blocks diff --git a/src/me/zombie_striker/pixelprinter/PixelPrinter.java b/src/me/zombie_striker/pixelprinter/PixelPrinter.java index 48b0524..84c4c28 100644 --- a/src/me/zombie_striker/pixelprinter/PixelPrinter.java +++ b/src/me/zombie_striker/pixelprinter/PixelPrinter.java @@ -17,27 +17,51 @@ import java.awt.Color; import java.awt.image.BufferedImage; -import java.io.*; -import java.net.*; -import java.util.*; - -import javax.imageio.*; - -import me.zombie_striker.pixelprinter.data.*; -import me.zombie_striker.pixelprinter.util.*; -import me.zombie_striker.pluginconstructor.*; -import me.zombie_striker.pluginconstructor.RGBBlockColor.Pixel; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.bukkit.*; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import me.zombie_striker.pixelprinter.data.FileCreatorData; +import me.zombie_striker.pixelprinter.util.AsyncImageHolder; +import me.zombie_striker.pixelprinter.util.GifHolder; +import me.zombie_striker.pixelprinter.util.SkinCreator; +import me.zombie_striker.pluginconstructor.Direction; +import me.zombie_striker.pluginconstructor.MapWallUtil; +import me.zombie_striker.pluginconstructor.MaterialData; +import me.zombie_striker.pluginconstructor.MojangAPI; +import me.zombie_striker.pluginconstructor.PluginConstructorAPI; +import me.zombie_striker.pluginconstructor.RGBBlockColor; +import me.zombie_striker.pluginconstructor.RGBBlockColor.Pixel; +import me.zombie_striker.pluginconstructor.RGBChatColor; +import me.zombie_striker.pluginconstructor.ReflectionUtilREMOVELATEER; + public class PixelPrinter extends JavaPlugin { private String prefix = ChatColor.DARK_PURPLE + "[PixelPrinter]" + ChatColor.WHITE; @@ -50,6 +74,8 @@ public class PixelPrinter extends JavaPlugin { private static PixelPrinter instance; + public static boolean isAbove113 = false; + private static int skin_creator_delay = 0; public static PixelPrinter getInstance() { @@ -72,8 +98,10 @@ public void onEnable() { // Download the API dependancy try { if (Bukkit.getPluginManager().getPlugin("PluginConstructorAPI") == null) - GithubDependDownloader.autoUpdate(this, new File(getDataFolder().getParentFile(),"PluginConstructorAPI.jar"), "ZombieStriker", "PluginConstructorAPI", "PluginConstructorAPI.jar"); - //new DependencyDownloader(this, 276723); + GithubDependDownloader.autoUpdate(this, + new File(getDataFolder().getParentFile(), "PluginConstructorAPI.jar"), "ZombieStriker", + "PluginConstructorAPI", "PluginConstructorAPI.jar"); + // new DependencyDownloader(this, 276723); } catch (Exception e) { e.printStackTrace(); } @@ -86,8 +114,8 @@ public void run() { }.runTaskTimer(this, 0, 10); instance = this; - images = new File(getDataFolder() ,"images"); - resoucepackFolder = new File(getDataFolder() ,"custom_textures"); + images = new File(getDataFolder(), "images"); + resoucepackFolder = new File(getDataFolder(), "custom_textures"); Bukkit.getPluginManager().registerEvents(new PPListener(this), this); if (!getDataFolder().exists()) @@ -106,6 +134,7 @@ public void run() { try { PluginConstructorAPI.loadCustomTextures(resoucepackFolder); } catch (NoClassDefFoundError e) { + e.printStackTrace(); } initHelp(); @@ -127,8 +156,7 @@ public void run() { if (!getConfig().contains("whitelistedMaterialsEnabled") || !getConfig().contains("whitelistedMaterials")) { supportedMaterials = null; getConfig().set("whitelistedMaterialsEnabled", false); - getConfig().set("whitelistedMaterials", - Arrays.asList(Material.STONE.name(), Material.WOOL.name(), Material.NETHERRACK.name())); + getConfig().set("whitelistedMaterials", Arrays.asList(Material.STONE.name(), Material.NETHERRACK.name())); List fullanmes = new ArrayList<>(); for (Material m : Material.values()) { if (m.isBlock()) @@ -147,6 +175,13 @@ public void run() { } catch (Error | Exception e) { } } + } else { + supportedMaterials = null; + } + try { + isAbove113 = ReflectionUtilREMOVELATEER.isVersionHigherThan(1, 13); + } catch (Error | Exception e45) { + } // bStats metrics @@ -175,37 +210,44 @@ public String call() throws Exception { public void run() { for (GifHolder gif : gifs) { - nearPlayers = false; - if (gif == null) { - System.out.println("gif is null"); - continue; - } - if (gif.isLoaded()) { - for (Player p : Bukkit.getOnlinePlayers()) { - if (p.getLocation().distance(gif.getMinCorner()) < 300) { - nearPlayers = true; - break; - } + try { + nearPlayers = false; + if (gif == null) { + System.out.println("gif is null"); + continue; } - if (nearPlayers) { - gif.loadFrame(); - // gif.loadFrame(); + if (gif.isLoaded()) { + for (Player p : Bukkit.getOnlinePlayers()) { + if (p.getLocation().distance(gif.getMinCorner()) < 300) { + nearPlayers = true; + break; + } + } + if (nearPlayers) { + gif.loadFrame(); + // gif.loadFrame(); + } } + } catch (Exception e45) { + e45.printStackTrace(); } } } // TODO: Doubling loadframe and the delay so it is less taxing on // low end computers - }, 10, 8); + }, 10, 4); - /* final Updater updater = new Updater(instance, 98985, getConfig().getBoolean("auto-update"));*/ + /* + * final Updater updater = new Updater(instance, 98985, + * getConfig().getBoolean("auto-update")); + */ /* * new BukkitRunnable() { public void run() { // TODO: Works well. Make changes * for the updaters of // PixelPrinter and Music later. if * (updater.updaterActive) updater.download(false); } * }.runTaskTimerAsynchronously(this, 20 /* * 60 * /, 20 * 60 * 5); */ - GithubUpdater.autoUpdate(this, "ZombieStriker", "PixelPrinter","PixelPrinter.jar"); + GithubUpdater.autoUpdate(this, "ZombieStriker", "PixelPrinter", "PixelPrinter.jar"); } public void onDisable() { @@ -354,19 +396,39 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String // ======================================================================DEBUG else if (args[0].equalsIgnoreCase("debug")) { - - // for(int i = 0; i < 20; i ++){ - // sender.sendMessage(ChatColor.values()[i]+" :::: MM OO XX $$ CC ** - // "+BLACK_BAR+BLACK_BAR); - // } - final Player player = (Player) sender; - new BukkitRunnable() { - - @Override - public void run() { - player.sendMessage(RGBBlockColor.getFurthestColor() + ""); + StringBuilder sb = new StringBuilder(); + List loaded = new ArrayList(); + for (MaterialData k : RGBBlockColor.materialValue.keySet()) { + if (!loaded.contains(k.getMaterial())) + loaded.add(k.getMaterial()); + } + // int color = 0; + List mat = new ArrayList(); + for (Material m : Material.values()) { + if (m.isBlock() && !m.isLegacy() && !loaded.contains(m) && !m.name().endsWith("SHULKER_BOX") + && !m.name().endsWith("CORAL_FAN") && !m.name().endsWith("SAPLING") + && !m.name().endsWith("_BANNER") && !m.name().endsWith("_HEAD") + && !m.name().endsWith("CARPET") && !m.name().endsWith("STAINED_GLASS") + && !m.name().endsWith("_TRAPDOOR") && !m.name().endsWith("RAIL") + && !m.name().startsWith("INFESTED") && !m.name().endsWith("_BED") + && !m.name().endsWith("_SLAB") && !m.name().endsWith("_STAIRS") + && !m.name().endsWith("GLASS_PANE") && !m.name().endsWith("FENCE") + && !m.name().endsWith("FENCE_GATE") && !m.name().endsWith("_BUTTON") + && !m.name().endsWith("PRESSURE_PLATE") && !m.name().endsWith("_DOOR")) { + mat.add(m); + // sb.append(ccc[color] + m.name() + ", &"+ChatColor.RESET.getChar()); + // color = (color+1) % ccc.length; } - }.runTaskLaterAsynchronously(this, 0); + } + int size = ((mat.size() / 9) + 1) * 9; + if (size > 9 * 14) + size = 9 * 14; + Inventory test = Bukkit.createInventory(null, size); + for (Material a : mat) + test.addItem(new ItemStack(a)); + ((Player) sender).openInventory(test); + // Bukkit.broadcastMessage("unused " + + // ChatColor.translateAlternateColorCodes('&', sb.toString())); } else if (args.length > 0 && args[0].equalsIgnoreCase("list")) { sender.sendMessage(getPrefix() + " All the saved images:"); @@ -686,6 +748,7 @@ public void run() { } catch (Exception e) { sender.sendMessage( getPrefix() + " Something failed. Please check console for more details."); + e.printStackTrace(); } } } catch (Exception e) { @@ -838,7 +901,7 @@ public void createMapAnim(Direction dir, Player p, BufferedImage[] bi2, int heig ItemStack[][] im = MapWallUtil.getMaps(bi2); for (int x = 0; x < im.length; x++) { for (int y = 0; y < im[x].length; y++) { - MapWallUtil.setBlockAt(dir, p, y, x, im[x][im[x].length - y - 1]); + MapWallUtil.setBlockAt(dir, p, y, x, im[x][(im[x].length - 1) - y]); } } } diff --git a/src/me/zombie_striker/pixelprinter/data/DataHolder.java b/src/me/zombie_striker/pixelprinter/data/DataHolder.java new file mode 100644 index 0000000..11496a5 --- /dev/null +++ b/src/me/zombie_striker/pixelprinter/data/DataHolder.java @@ -0,0 +1,58 @@ +package me.zombie_striker.pixelprinter.data; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.scheduler.BukkitRunnable; + +import me.zombie_striker.pixelprinter.PixelPrinter; +import me.zombie_striker.pluginconstructor.MaterialData; + +public class DataHolder implements ConfigurationSerializable { + public MaterialData md; + public Location b; + boolean hasFaces = false; + + public DataHolder(Location b2, MaterialData md) { + this.b = b2; + this.md = md; + } + public DataHolder(Location b, MaterialData md, boolean hasFaces) { + this.b = b; + this.md = md; + this.hasFaces = hasFaces; + } + + public DataHolder(Map data) { + final Map tempData = data; + new BukkitRunnable() { + + @Override + public void run() { + if (Bukkit.getWorld((String) tempData.get("b.w")) != null) { + b = new Location(Bukkit.getWorld((String) tempData.get("b.w")), (int) tempData.get("b.x"), + (int) tempData.get("b.y"), (int) tempData.get("b.z")); + cancel(); + } + } + }.runTaskTimer(PixelPrinter.getInstance(), 0, 20); + this.md = (MaterialData) data.get("md"); + } + + @Override + public Map serialize() { + Map data = new HashMap(); + data.put("b.x", this.b.getBlockX()); + data.put("b.y", this.b.getBlockY()); + data.put("b.z", this.b.getBlockZ()); + data.put("b.w", this.b.getWorld().getName()); + data.put("md", this.md); + return data; + } + public boolean hasFaces() { + return hasFaces; + } +} diff --git a/src/me/zombie_striker/pixelprinter/util/AsyncImageHolder.java b/src/me/zombie_striker/pixelprinter/util/AsyncImageHolder.java index 6ea23b4..06c6199 100644 --- a/src/me/zombie_striker/pixelprinter/util/AsyncImageHolder.java +++ b/src/me/zombie_striker/pixelprinter/util/AsyncImageHolder.java @@ -29,6 +29,7 @@ import me.zombie_striker.pluginconstructor.RGBBlockColor.Pixel; import org.bukkit.*; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.entity.Player; @@ -49,7 +50,7 @@ public AsyncImageHolder(Pixel[][] result1, Player p1, Location loc1, Direction d neg = isMinUpNeg(dir); moving = isMovingX(dir); this.enableTransparent = enableTrans; - + } @SuppressWarnings("deprecation") @@ -68,14 +69,14 @@ public void run() { for (int width = 0; width < (bi.getWidth()); width += 2) { for (int height = (bi.getHeight() - 1); height >= 0; height -= 2) { Location b = getBlockAt(height, width, bi.getHeight()); - if (b == null) { + if (b == null || b.getY() > 260) { continue; } Color[] color = new Color[4]; for (int i = 0; i < 4; i++) { int y = (height + 1 < result.length) ? height + (i % 2) : height; int x = (width + 1 < result[y].length) ? width + (i % 2) : width; - color[i] = new Color(result[y][x].r, result[y][x].g, result[y][x].b,result[y][x].a); + color[i] = new Color(result[y][x].r, result[y][x].g, result[y][x].b, result[y][x].a); } MaterialData m = RGBBlockColor.getClosestBlockValue(color, (dir == Direction.FLAT_NORTHEAST || dir == Direction.FLAT_NORTHWEST @@ -84,10 +85,12 @@ public void run() { String tempkey = (b.getBlockX() / 16) + "," + (b.getBlockZ() / 16); if (chunksorter.containsKey(tempkey)) { List temp = chunksorter.get(tempkey); + if(temp==null) + temp = new ArrayList(); temp.add(new DataHolder(b, m, m.hasDirection())); chunksorter.put(tempkey, temp); } else { - List temp = new ArrayList(); + List temp = new ArrayList(); temp.add(new DataHolder(b, m)); chunksorter.put(tempkey, temp); } @@ -100,7 +103,7 @@ public void run() { final IntHolder blocksUpdated = new IntHolder(); final IntHolder didNotHaveToReplace = new IntHolder(); - + for (Entry> ent : chunksorter.entrySet()) { final List gg = ent.getValue(); timesTicked++; @@ -108,158 +111,71 @@ public void run() { delayLoadingMessage %= maxDelay; final int currTick = timesTicked; - new BukkitRunnable() { @Override public void run() { - for (DataHolder dh : gg) { - BlockState bs = dh.b.getBlock().getState(); + for (final DataHolder dh : gg) { + final BlockState bs = dh.b.getBlock().getState(); if (dh.md.getMaterial() != Material.AIR) { byte rd = dh.md.getData(); + BlockFace bf = null; - if (dh.hasFaces) { - try { - if (dh.md.getMaterial().name().endsWith("_DOOR")&&!dh.md.getMaterial().name().contains("TRAP")) { - // org.bukkit.material.Door door = (org.bukkit.material.Door) - // bs.getData(); - if (dir == Direction.UP_NORTH) - rd = ((byte) 2); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 0); - if (dir == Direction.UP_WEST) - rd = ((byte) 1); - } - if (dh.md.getMaterial()== Material.FURNACE - || dh.md.getMaterial()== Material.BURNING_FURNACE) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 5); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 4); - if (dir == Direction.UP_WEST) - rd = ((byte) 2); - - } - if (dh.md.getMaterial() == Material.DISPENSER) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 5); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 4); - if (dir == Direction.UP_WEST) - rd = ((byte) 2); - - } - if (dh.md.getMaterial()== Material.PISTON_BASE - || dh.md.getMaterial()== Material.PISTON_STICKY_BASE) { - if (dh.md.getDirection() == BlockFace.UP) { - if (dir == Direction.UP_NORTH) - rd = ((byte) 5); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 4); - if (dir == Direction.UP_WEST) - rd = ((byte) 2); - } - if (dh.md.getDirection() == BlockFace.WEST) { - if (dir == Direction.UP_NORTH) - rd = ((byte) 4); - if (dir == Direction.UP_EAST) - rd = ((byte) 2); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 5); - if (dir == Direction.UP_WEST) - rd = ((byte) 3); - } - } - if (dh.md.getMaterial() == Material.PUMPKIN - || dh.md.getMaterial() == Material.JACK_O_LANTERN) { - if (dh.md.getDirection() == BlockFace.EAST) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 3); - if (dir == Direction.UP_EAST) - rd = ((byte) 0); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 1); - if (dir == Direction.UP_WEST) - rd = ((byte) 2); - } - - if (dh.md.getDirection() == BlockFace.WEST) { - if (dir == Direction.UP_NORTH) - rd = ((byte) 0); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 2); - if (dir == Direction.UP_WEST) - rd = ((byte) 1); - } - - } - - if (dh.md.getMaterial()== Material.OBSERVER) { - if (dh.md.getDirection() == BlockFace.EAST) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 4); - if (dir == Direction.UP_EAST) - rd = ((byte) 2); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 5); - if (dir == Direction.UP_WEST) - rd = ((byte) 3); - } - - if (dh.md.getDirection() == BlockFace.WEST) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 5); - if (dir == Direction.UP_EAST) - rd = ((byte) 3); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 4); - if (dir == Direction.UP_WEST) - rd = ((byte) 2); - } - - if (dh.md.getDirection() == BlockFace.NORTH) { - // Go eat, then we need south. - // Go south, then face west, ect. - if (dir == Direction.UP_NORTH) - rd = ((byte) 3); - if (dir == Direction.UP_EAST) - rd = ((byte) 5); - if (dir == Direction.UP_SOUTH) - rd = ((byte) 2); - if (dir == Direction.UP_WEST) - rd = ((byte) 4); + if (dh.hasFaces()) { + bf = getBlockFace(dh, dir); + rd=getBlockData(dh, dir); + } + if (dh.md.getMaterial() != bs.getType() + || (((int) bs.getRawData()) != ((int) rd))) { + if (PixelPrinter.isAbove113) { + bs.getBlock().setType(dh.md.getMaterial()); + try { + if (bf != null) { + org.bukkit.block.data.Directional d = ((org.bukkit.block.data.Directional) bs + .getBlock().getBlockData()); + d.setFacing(bf); + bs.getBlock().setBlockData(d); } + } catch (Error | Exception e45) { + e45.printStackTrace(); } - } catch (Error | Exception e54) { + } else { + bs.setType(dh.md.getMaterial()); + if (bs.getRawData() != rd) + bs.setRawData(rd); + bs.update(true, false); + } + if (dh.md.getMaterial().hasGravity()) { + Block below = bs.getBlock().getLocation().subtract(0, 1, 0).getBlock(); + if (below.getType() == Material.AIR) + below.setType(Material.STONE); } - } - if (dh.md.getMaterial() != bs.getType() || (((int)bs.getRawData()) != ((int)rd))) { - bs.setType(dh.md.getMaterial()); - bs.setRawData(rd); - bs.update(true, false); blocksUpdated.setI(blocksUpdated.getI() + 1); - }else { + final BlockFace bf2 = bf; + new BukkitRunnable() { + + @Override + public void run() { + if (bs.getBlock().getType() != dh.md.getMaterial() + || (PixelPrinter.isAbove113 + ? (bf2 != null + ? (((org.bukkit.block.data.Directional) bs + .getBlock().getBlockData()) + .getFacing() != bf2) + : false) + : bs.getBlock().getData() != dh.md.getData()) + || bs.getBlock().getType() == Material.AIR) + Bukkit.broadcastMessage(PixelPrinter.getInstance().getPrefix() + + "Incorrect value: " + dh.md.getMaterial().name() + ":" + + dh.md.getData() + " is " + bs.getBlock().getType() + ":" + + bs.getBlock().getData() + " at " + + bs.getBlock().getLocation().getBlockX() + "," + + bs.getBlock().getLocation().getBlockY() + "," + + bs.getBlock().getLocation().getBlockZ()); + } + }.runTaskLater(PixelPrinter.getInstance(), 20 * 3); + } else { didNotHaveToReplace.setI(2); } } @@ -277,7 +193,9 @@ public void run() { @Override public void run() { for (Player p2 : minCorner.getWorld().getPlayers()) { - p2.sendMessage(PixelPrinter.getInstance().getPrefix() + " Done!"+(didNotHaveToReplace.getI()==2?" Updated "+blocksUpdated.getI()+" blocks.":"")); + p2.sendMessage(PixelPrinter.getInstance().getPrefix() + " Done!" + + (didNotHaveToReplace.getI() == 2 ? " Updated " + blocksUpdated.getI() + " blocks." + : "")); } } }.runTaskLater(PixelPrinter.getInstance(), 3 * timesTicked); @@ -357,7 +275,7 @@ public void run() { */ } - class DataHolder { + /*class DataHolder { MaterialData md; Location b; boolean hasFaces = false; @@ -371,6 +289,222 @@ public DataHolder(Location b, MaterialData md, boolean hasFaces) { this.md = md; this.hasFaces = hasFaces; } + public boolean hasFaces() { + return hasFaces; + } + }*/ + + public static BlockFace getBlockFace(DataHolder dh, Direction dir) { + + try { + if (dh.md.getMaterial() == Material.FURNACE || dh.md.getMaterial().name().equals("BURNING_FURNACE")) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_EAST) + return BlockFace.SOUTH; + if (dir == Direction.UP_SOUTH) + return BlockFace.WEST; + if (dir == Direction.UP_WEST) + return BlockFace.NORTH; + if (dir == Direction.UP_NORTH) + return BlockFace.EAST; + } + if (dh.md.getMaterial() == Material.DISPENSER) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_EAST) + return BlockFace.SOUTH; + if (dir == Direction.UP_SOUTH) + return BlockFace.WEST; + if (dir == Direction.UP_WEST) + return BlockFace.NORTH; + if (dir == Direction.UP_NORTH) + return BlockFace.EAST; + + } + + if (dh.md.getMaterial() == Material.OBSERVER) { + if (dh.md.getDirection() == BlockFace.EAST) { + if (PixelPrinter.isAbove113) { + if (dir == Direction.UP_EAST) + return BlockFace.SOUTH; + if (dir == Direction.UP_SOUTH) + return BlockFace.WEST; + if (dir == Direction.UP_WEST) + return BlockFace.NORTH; + if (dir == Direction.UP_NORTH) + return BlockFace.EAST; + } + + if (dh.md.getDirection() == BlockFace.WEST) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_EAST) + return BlockFace.NORTH; + if (dir == Direction.UP_SOUTH) + return BlockFace.EAST; + if (dir == Direction.UP_WEST) + return BlockFace.SOUTH; + if (dir == Direction.UP_NORTH) + return BlockFace.WEST; + } + + if (dh.md.getDirection() == BlockFace.NORTH) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_EAST) + return BlockFace.EAST; + if (dir == Direction.UP_SOUTH) + return BlockFace.SOUTH; + if (dir == Direction.UP_WEST) + return BlockFace.WEST; + if (dir == Direction.UP_NORTH) + return BlockFace.NORTH; + } + } + } + } catch (Error | Exception e54) { + } + return null; } + public static byte getBlockData(DataHolder dh, Direction dir) { + + try { + if (dh.md.getMaterial().name().endsWith("_DOOR") && !dh.md.getMaterial().name().contains("TRAP")) { + // org.bukkit.material.Door door = (org.bukkit.material.Door) + // bs.getData(); + if (dir == Direction.UP_NORTH) + return ((byte) 2); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 0); + if (dir == Direction.UP_WEST) + return ((byte) 1); + } + if (dh.md.getMaterial() == Material.FURNACE || dh.md.getMaterial().name().equals("BURNING_FURNACE")) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 5); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 4); + if (dir == Direction.UP_WEST) + return ((byte) 2); + } + + if (dh.md.getMaterial() == Material.DISPENSER) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 5); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 4); + if (dir == Direction.UP_WEST) + return ((byte) 2); + + + } + if (dh.md.getMaterial().name().equals("PISTON_BASE") + || dh.md.getMaterial().name().equals("PISTON_STICKY_BASE")) { + if (dh.md.getDirection() == BlockFace.UP) { + if (dir == Direction.UP_NORTH) + return ((byte) 5); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 4); + if (dir == Direction.UP_WEST) + return ((byte) 2); + } + if (dh.md.getDirection() == BlockFace.WEST) { + if (dir == Direction.UP_NORTH) + return ((byte) 4); + if (dir == Direction.UP_EAST) + return ((byte) 2); + if (dir == Direction.UP_SOUTH) + return ((byte) 5); + if (dir == Direction.UP_WEST) + return ((byte) 3); + } + } + if (dh.md.getMaterial() == Material.PUMPKIN || dh.md.getMaterial() == Material.JACK_O_LANTERN) { + if (dh.md.getDirection() == BlockFace.EAST) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 3); + if (dir == Direction.UP_EAST) + return ((byte) 0); + if (dir == Direction.UP_SOUTH) + return ((byte) 1); + if (dir == Direction.UP_WEST) + return ((byte) 2); + } + + if (dh.md.getDirection() == BlockFace.WEST) { + if (dir == Direction.UP_NORTH) + return ((byte) 0); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 2); + if (dir == Direction.UP_WEST) + return ((byte) 1); + } + + } + + if (dh.md.getMaterial() == Material.OBSERVER) { + if (dh.md.getDirection() == BlockFace.EAST) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 4); + if (dir == Direction.UP_EAST) + return ((byte) 2); + if (dir == Direction.UP_SOUTH) + return ((byte) 5); + if (dir == Direction.UP_WEST) + return ((byte) 3); + + } + + if (dh.md.getDirection() == BlockFace.WEST) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 5); + if (dir == Direction.UP_EAST) + return ((byte) 3); + if (dir == Direction.UP_SOUTH) + return ((byte) 4); + if (dir == Direction.UP_WEST) + return ((byte) 2); + } + + + if (dh.md.getDirection() == BlockFace.NORTH) { + // Go eat, then we need south. + // Go south, then face west, ect. + if (dir == Direction.UP_NORTH) + return ((byte) 3); + if (dir == Direction.UP_EAST) + return ((byte) 5); + if (dir == Direction.UP_SOUTH) + return ((byte) 2); + if (dir == Direction.UP_WEST) + return ((byte) 4); + } + + } + } catch (Error | Exception e54) { + } + return dh.md.getData(); + } } diff --git a/src/me/zombie_striker/pixelprinter/util/GifHolder.java b/src/me/zombie_striker/pixelprinter/util/GifHolder.java index 44b1c5b..91a8b03 100644 --- a/src/me/zombie_striker/pixelprinter/util/GifHolder.java +++ b/src/me/zombie_striker/pixelprinter/util/GifHolder.java @@ -32,6 +32,8 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.scheduler.BukkitRunnable; @@ -62,11 +64,9 @@ public static void registerClass() { public static List freeID = new ArrayList();; - public GifHolder(String filename, Location minLocation, int height, - String dir, UUID owner) { + public GifHolder(String filename, Location minLocation, int height, String dir, UUID owner) { this.dir = Direction.getDir(dir); - this.p = Bukkit.getPlayer(owner) == null ? "null" : Bukkit.getPlayer( - owner).getName(); + this.p = Bukkit.getPlayer(owner) == null ? "null" : Bukkit.getPlayer(owner).getName(); this.minCorner = minLocation; this.height = height * 2; this.fileName = filename; @@ -78,8 +78,8 @@ public GifHolder(String filename, Location minLocation, int height, break; } } - createFrames(new File(PixelPrinter.getInstance().getImageFile() - + File.separator + this.fileName), this.height, owner); + createFrames(new File(PixelPrinter.getInstance().getImageFile() + File.separator + this.fileName), this.height, + owner); } public void init() { @@ -103,13 +103,14 @@ public void run() { holders.clear(); final BufferedImage bi = getFrames()[frame]; - final Pixel[][] result = RGBBlockColor - .convertTo2DWithoutUsingGetRGB(bi); + final Pixel[][] result = RGBBlockColor.convertTo2DWithoutUsingGetRGB(bi); for (int width = 0; width < bi.getWidth(); width += 2) { for (int height = (bi.getHeight() - 1); height >= 0; height -= 2) { - Location b = getBlockAt(height, width, - bi.getHeight()); + if (width / 2 >= fWidth || height / 2 >= fHeight) + continue; + + Location b = getBlockAt(height, width, bi.getHeight()); Color[] color = new Color[4]; boolean allTrans = true; @@ -118,52 +119,36 @@ public void run() { int defaultB = 0; for (int iT = 0; iT < 4; iT++) { - int yT = (1 + height < result.length) ? height - + (iT % 2) : height; - int xT = (width + 1 < result[height].length) ? width - + (iT / 2) - : width; - Pixel rgb = result[yT][xT]; - if (rgb.r != 0 - || rgb.g != 0 - || rgb.b != 0) { + int yT = (1 + height < result.length) ? height + (iT % 2) : height; + int xT = (width + 1 < result[height].length) ? width + (iT / 2) : width; + Pixel rgb = result[yT][xT]; + if (rgb.r != 0 || rgb.g != 0 || rgb.b != 0) { allTrans = false; - defaultR =rgb.r; + defaultR = rgb.r; defaultG = rgb.g; defaultB = rgb.b; break; } } - if (allTrans){ + if (allTrans) { isTrans[frame][width / 2][height / 2] = true; - }else { + } else { for (int i = 0; i < 4; i++) { - int y = (1 + height < result.length) ? height - + (i % 2) - : height; - int x = (width + 1 < result[height].length) ? width - + (i / 2) - : width; - Pixel rgb = result[y][x]; - if (rgb .r != 0 - || rgb .g != 0 - || rgb .b != 0) { - color[i] = new Color(rgb.r, - rgb.g, rgb.b); + int y = (1 + height < result.length) ? height + (i % 2) : height; + int x = (width + 1 < result[height].length) ? width + (i / 2) : width; + Pixel rgb = result[y][x]; + if (rgb.r != 0 || rgb.g != 0 || rgb.b != 0) { + color[i] = new Color(rgb.r, rgb.g, rgb.b); } else { - color[i] = new Color(defaultR, - defaultG, defaultB); + color[i] = new Color(defaultR, defaultG, defaultB); } } - MaterialData m = RGBBlockColor - .getClosestBlockValue( - color, - (dir == Direction.FLAT_NORTHEAST - || dir == Direction.FLAT_NORTHWEST - || dir == Direction.FLAT_SOUTHEAST || dir == Direction.FLAT_SOUTHWEST)); + MaterialData m = RGBBlockColor.getClosestBlockValue(color, + (dir == Direction.FLAT_NORTHEAST || dir == Direction.FLAT_NORTHWEST + || dir == Direction.FLAT_SOUTHEAST || dir == Direction.FLAT_SOUTHWEST)); DataHolder dh = new DataHolder(b, m); holders.add(dh); - materials[frame][width / 2][height / 2] = dh; + materials[frame][width / 2][height / 2] = dh; } } } @@ -193,6 +178,7 @@ public void setEastOrWest(boolean b) { public void loadFrame() { if (getFrames() == null || getFrames().length < 1) return; + for (int x = 0; x < materials[currentFrame].length; x++) { for (int y = materials[currentFrame][x].length - 1; y >= 0; y--) { if (!isTrans[currentFrame][x][y]) { @@ -200,14 +186,37 @@ public void loadFrame() { if (dh == null) continue; Block b = dh.b.getBlock(); - if (b.getType() != dh.md.getMaterial()) - b.setType(dh.md.getMaterial()); - try { - if (b.getData() != dh.md.getData() - && dh.md.getData() != 0) - b.setData(dh.md.getData()); - } catch (Exception e) { - e.printStackTrace(); + if (b.getType() != dh.md.getMaterial()) { + BlockState state = b.getState(); + BlockFace bf=null; + byte rd = dh.md.getData(); + if (dh.hasFaces()) { + bf = AsyncImageHolder.getBlockFace(dh, dir); + rd=AsyncImageHolder.getBlockData(dh, dir); + } + if (PixelPrinter.isAbove113) { + b.setType(dh.md.getMaterial()); + try { + if (bf != null) { + org.bukkit.block.data.Directional d = ((org.bukkit.block.data.Directional) b.getBlockData()); + d.setFacing(bf); + b.setBlockData(d); + } + } catch (Error | Exception e45) { + e45.printStackTrace(); + } + } else { + state.setType(dh.md.getMaterial()); + //b.setType(dh.md.getMaterial()); + try { + if (dh.md.getData() != 0&&b.getData() != rd) + state.setRawData(rd); + //b.setData(dh.md.getData()); + } catch (Exception e) { + e.printStackTrace(); + } + state.update(true, false); + } } } } @@ -275,48 +284,38 @@ public GifHolder(Map data) { this.moving = (boolean) data.get("moving"); this.neg = (boolean) data.get("neg"); this.owner = UUID.fromString((String) data.get("owner")); - createFrames(new File(PixelPrinter.getInstance().getImageFile() - + File.separator + this.fileName), this.height, owner); + createFrames(new File(PixelPrinter.getInstance().getImageFile() + File.separator + this.fileName), this.height, + owner); final IntHolder temp = new IntHolder(); - temp.setI(Bukkit.getScheduler().scheduleSyncRepeatingTask( - PixelPrinter.getInstance(), new Runnable() { - public void run() { - if (Bukkit.getWorld((String) tempData - .get("minCorner.w")) != null) { - minCorner = Bukkit - .getWorld( - (String) tempData - .get("minCorner.w")) - .getBlockAt( - (int) tempData.get("minCorner.x"), - (int) tempData.get("minCorner.y"), - (int) tempData.get("minCorner.z")) - .getLocation(); - Bukkit.getScheduler().cancelTask(temp.getI()); - } - } - }, 0, 20)); + temp.setI(Bukkit.getScheduler().scheduleSyncRepeatingTask(PixelPrinter.getInstance(), new Runnable() { + public void run() { + if (Bukkit.getWorld((String) tempData.get("minCorner.w")) != null) { + minCorner = Bukkit.getWorld((String) tempData.get("minCorner.w")) + .getBlockAt((int) tempData.get("minCorner.x"), (int) tempData.get("minCorner.y"), + (int) tempData.get("minCorner.z")) + .getLocation(); + Bukkit.getScheduler().cancelTask(temp.getI()); + } + } + }, 0, 20)); final IntHolder temp2 = new IntHolder(); - temp2.setI(Bukkit.getScheduler().scheduleSyncRepeatingTask( - PixelPrinter.getInstance(), new Runnable() { - public void run() { - if (minCorner != null && getFrames() != null - && getFrames().length > 1) { - init(); - Bukkit.getScheduler().cancelTask(temp2.getI()); - } - } - }, 0, 20)); + temp2.setI(Bukkit.getScheduler().scheduleSyncRepeatingTask(PixelPrinter.getInstance(), new Runnable() { + public void run() { + if (minCorner != null && getFrames() != null && getFrames().length > 1) { + init(); + Bukkit.getScheduler().cancelTask(temp2.getI()); + } + } + }, 0, 20)); } @SuppressWarnings("deprecation") public void createFrames(final File gif, final int height, UUID owner) { - Bukkit.getScheduler().scheduleAsyncDelayedTask( - PixelPrinter.getInstance(), new Runnable() { - public void run() { - setFrames(getFrames(gif, height)); - } - }, 0); + Bukkit.getScheduler().scheduleAsyncDelayedTask(PixelPrinter.getInstance(), new Runnable() { + public void run() { + setFrames(getFrames(gif, height)); + } + }, 0); } @@ -341,16 +340,15 @@ public static BufferedImage[] getFrames(URL gif, int height) { private static BufferedImage[] getFrames(ImageInputStream gif, int height) { try { - ImageReader reader = (ImageReader) ImageIO - .getImageReadersByFormatName("gif").next(); + ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName("gif").next(); reader.setInput((ImageInputStream) gif, true); Iterator iter = reader.readAll(null); List bii = new ArrayList<>(); while (iter.hasNext()) { IIOImage img = iter.next(); BufferedImage frame = (BufferedImage) img.getRenderedImage(); - frame = RGBBlockColor.resize(frame, (int) (frame.getWidth() - * ((double) height) / frame.getHeight()), height);// .createResizedCopy(frame, + frame = RGBBlockColor.resize(frame, (int) (frame.getWidth() * ((double) height) / frame.getHeight()), + height);// .createResizedCopy(frame, // height, false); bii.add(frame); } @@ -373,41 +371,3 @@ private static BufferedImage[] getFrames(ImageInputStream gif, int height) { } } -class DataHolder implements ConfigurationSerializable { - MaterialData md; - Location b; - - public DataHolder(Location b2, MaterialData md) { - this.b = b2; - this.md = md; - } - - public DataHolder(Map data) { - final Map tempData = data; - final IntHolder temp = new IntHolder(); - temp.setI(Bukkit.getScheduler().scheduleSyncRepeatingTask( - PixelPrinter.getInstance(), new Runnable() { - public void run() { - if (Bukkit.getWorld((String) tempData.get("b.w")) != null) { - b = new Location(Bukkit.getWorld((String) tempData - .get("b.w")), (int) tempData.get("b.x"), - (int) tempData.get("b.y"), (int) tempData - .get("b.z")); - Bukkit.getScheduler().cancelTask(temp.getI()); - } - } - }, 0, 20)); - this.md = (MaterialData) data.get("md"); - } - - @Override - public Map serialize() { - Map data = new HashMap(); - data.put("b.x", this.b.getBlockX()); - data.put("b.y", this.b.getBlockY()); - data.put("b.z", this.b.getBlockZ()); - data.put("b.w", this.b.getWorld().getName()); - data.put("md", this.md); - return data; - } -}