Merge remote-tracking branch 'origin/master'

This commit is contained in:
Shaun Bennett 2015-02-06 23:40:14 -05:00
commit c285fae035
6 changed files with 393 additions and 243 deletions

View File

@ -0,0 +1,30 @@
package mineplex.core.map;
import java.awt.Image;
import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ImageMapRenderer extends MapRenderer
{
private Image _image;
private boolean _first = true;
public ImageMapRenderer(BufferedImage image)
{
_image = image;
}
@Override
public void render(MapView view, MapCanvas canvas, Player player)
{
if (_image != null && _first)
{
canvas.drawImage(0, 0, _image);
_first = false;
}
}
}

View File

@ -1,46 +0,0 @@
package mineplex.core.map;
import java.awt.Image;
import java.net.URI;
import javax.imageio.ImageIO;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ImageRenderer extends MapRenderer
{
private boolean hasRendered;
private final Image theImg;
private Thread renderImageThread;
public final String theUrl;
public ImageRenderer(String url) throws Exception
{
hasRendered = false;
theImg = ImageIO.read(URI.create(url).toURL().openStream());
theUrl = url;
}
@Override
public void render(MapView view, final MapCanvas canvas, Player plyr)
{
if(!hasRendered && theImg != null && renderImageThread == null)
{
renderImageThread = new Thread()
{
@Override
public void run()
{
canvas.drawImage(0, 0, MapPalette.resizeImage(theImg));
}
};
renderImageThread.start();
hasRendered = true;
}
}
}

View File

@ -1,138 +1,388 @@
package mineplex.core.map;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.map.commands.MapImage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.HashMap;
import javax.imageio.ImageIO;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilServer;
import net.minecraft.server.v1_7_R4.EntityItemFrame;
import net.minecraft.server.v1_7_R4.PersistentCollection;
import net.minecraft.server.v1_7_R4.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.server.MapInitializeEvent;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin;
public class Map extends MiniPlugin
public class Map implements Listener
{
JavaPlugin Plugin;
//Private
private Player _caller = null;
private String _url = "http://chivebox.com/img/mc/news.png";
private String _defaultUrl = null;
private HashMap<ItemFrame, MapView> _maps = new HashMap<ItemFrame, MapView>();
public Map(JavaPlugin plugin)
{
super("Map", plugin);
public Map(MiniPlugin plugin, String location, Block corner1, Block corner2)
{
this(plugin, location, corner1, corner2, null);
}
Plugin = plugin;
}
public Map(MiniPlugin plugin, String location, Block corner1, Block corner2, BlockFace facingDirection)
{
@Override
public void AddCommands()
{
addCommand(new MapImage(this));
}
public ItemStack GetMap()
{
return ItemStackFactory.Instance.CreateStack(Material.MAP, (byte)127, 1, C.cAqua + C.Bold + "iMap 3.0");
}
try
{
// Make sure the world doesn't change the mapcount information
PersistentCollection collection = ((CraftWorld) corner1.getWorld()).getHandle().worldMaps;
Field f = collection.getClass().getDeclaredField("a");
f.setAccessible(true);
f.set(collection, null);
}
catch (Exception ex)
{
ex.printStackTrace();
}
public void SpawnMap(Player caller, String[] args)
{
if (args == null || args.length == 0)
{
UtilPlayer.message(_caller, F.main("Map Image", "Missing Image URL!"));
return;
}
plugin.RegisterEvents(this);
_caller = caller;
_url = args[0];
caller.getInventory().addItem(GetMap());
if (args.length > 1)
{
if (args[1].equals("all"))
{
for (Player player : UtilServer.getPlayers())
{
if (player.equals(caller))
continue;
player.getInventory().remove(Material.MAP);
// Clear out the area to make room for the picture
{
int bX = Math.min(corner1.getX(), corner2.getX());
int bY = Math.min(corner1.getY(), corner2.getY());
int bZ = Math.min(corner1.getZ(), corner2.getZ());
int tX = Math.max(corner1.getX(), corner2.getX());
int tY = Math.max(corner1.getY(), corner2.getY());
int tZ = Math.max(corner1.getZ(), corner2.getZ());
player.getInventory().addItem(GetMap());
}
}
else
{
Player target = UtilPlayer.searchOnline(caller, args[1], true);
if (target != null)
{
target.getInventory().remove(Material.MAP);
target.getInventory().addItem(GetMap());
}
}
}
}
for (int x = bX; x <= tX; x++)
{
for (int y = bY; y <= tY; y++)
{
for (int z = bZ; z <= tZ; z++)
{
Block b = corner1.getWorld().getBlockAt(x, y, z);
@EventHandler
public void MapInit(MapInitializeEvent event)
{
//Map Setup
final MapView map = event.getMap();
if (!UtilBlock.airFoliage(b))
{
b.setType(Material.AIR);
}
}
}
}
for (MapRenderer rend : map.getRenderers())
map.removeRenderer(rend);
// Remove offending hanging pictures.
for (Hanging hanging : corner1.getWorld().getEntitiesByClass(Hanging.class))
{
Location loc = hanging.getLocation();
if (_defaultUrl != null)
{
Plugin.getServer().getScheduler().runTaskAsynchronously(Plugin, new Runnable()
{
public void run()
{
try
{
map.addRenderer(new ImageRenderer(_defaultUrl));
}
catch (Exception e)
{
System.out.println("Invalid Default Image: " + _defaultUrl);
}
if (loc.getX() >= bX && loc.getX() <= tX)
{
if (loc.getY() >= bY && loc.getY() <= tY)
{
if (loc.getZ() >= bZ && loc.getZ() <= tZ)
{
hanging.remove();
}
}
}
}
}
_defaultUrl = null;
}
});
if (facingDirection == null)
{
facingDirection = getImageFace(corner1, corner2);
}
}
else if (_url != null)
{
Plugin.getServer().getScheduler().runTaskAsynchronously(Plugin, new Runnable()
{
public void run()
{
try
{
map.addRenderer(new ImageRenderer(_url));
UtilPlayer.message(_caller, F.main("Map Image", "Loaded Image: " + _url));
}
catch (Exception e)
{
UtilPlayer.message(_caller, F.main("Map Image", "Invalid Image URL: " + _url));
}
}
});
}
}
// Get the very corner of the image
Block corner = new Location(
public void SetDefaultUrl(String string)
{
_defaultUrl = string;
}
corner1.getWorld(),
Math.min(corner1.getX(), corner2.getX()),
Math.min(corner1.getY(), corner2.getY()),
Math.min(corner1.getZ(), corner2.getZ()))
.getBlock();
// Find the dimensions of the image width
int x = corner2.getX() - corner1.getX();
int z = corner2.getZ() - corner1.getZ();
// Turn it into the single largest number. If there is a 0, we are going to have a problem.
int width = Math.max(Math.abs(x), Math.abs(z)) + 1;
// Get the image height
int height = Math.abs(corner1.getY() - corner2.getY()) + 1;
corner = corner.getRelative(facingDirection.getOppositeFace());
// Load image
BufferedImage image = loadImage(location);
if (image == null)
{
throw new IllegalArgumentException("Cannot load image at '" + location + "'");
}
// Resize image to fit into the dimensions
image = toBufferedImage(image.getScaledInstance(width * 128, height * 128, Image.SCALE_SMOOTH), image.getType());
// Find out the direction to cutup the picture
boolean reversed = facingDirection.getModZ() > 0 || facingDirection.getModX() < 0;
// Cut image into 128 pixels images
BufferedImage[] imgs = cutIntoPieces(image, width, height, !reversed);
drawImage(imgs, corner, Math.abs(x), height - 1, Math.abs(z), facingDirection);
}
private BufferedImage[] cutIntoPieces(BufferedImage image, int width, int height, boolean reversed)
{
BufferedImage[] pieces = new BufferedImage[width * height]; // Image array to hold image chunks
int count = 0;
for (int x1 = 0; x1 < width; x1++)
{
int x = reversed ? (width - 1) - x1 : x1;
for (int y = 0; y < height; y++)
{
// Initialize the image array with image chunks
pieces[count] = new BufferedImage(128, 128, image.getType());
// draws the image chunk
Graphics2D gr = pieces[count++].createGraphics();
gr.drawImage(image, 0, 0, 128, 128, x * 128, y * 128, (x * 128) + 128, (y * 128) + 128, null);
gr.dispose();
}
}
return pieces;
}
private void drawImage(BufferedImage[] images, Block cornerBlock, int xSize, int ySize, int zSize, BlockFace direction)
{
int count = 0;
// Turn each subimage into a map file
for (int x = 0; x <= xSize; x++)
{
for (int z = 0; z <= zSize; z++)
{
for (int y = ySize; y >= 0; y--)
{
// Create a itemframe and set the map inside
Block b = cornerBlock.getRelative(x, y, z);
setItemFrame(b, direction, images[count++]);
}
}
}
}
/**
* Get the direction the image needs to face
*/
private BlockFace getImageFace(Block c1, Block c2)
{
// Get the center block between the two locations
Block b = c1.getWorld().getBlockAt(
Math.min(c1.getX(), c2.getX()) + ((c1.getX() + c2.getX()) / 2),
Math.min(c1.getY(), c2.getY()) + ((c1.getY() + c2.getY()) / 2),
Math.min(c1.getZ(), c2.getZ()) + ((c1.getZ() + c2.getZ()) / 2));
// Using the block as a indicator on how the rest of the blocks look, lets scan the four directions to see what blocks are
// solid.
boolean xOk = c1.getX() == c2.getX();
boolean zOk = c1.getZ() == c2.getZ();
for (int x = -1; x <= 1; x++)
{
for (int z = -1; z <= 1; z++)
{
if (x != z && (x == 0 || z == 0))
{
Block b1 = b.getRelative(x, 0, z);
if (UtilBlock.solid(b1))
{
BlockFace face = b1.getFace(b);
if ((face.getModX() != 0 && xOk) || (face.getModZ() != 0 && zOk))
{
return face;
}
}
}
}
}
return xOk ? BlockFace.EAST : BlockFace.NORTH;
}
private MapView getMap(World world, BufferedImage image)
{
MapView map = Bukkit.createMap(world);
for (MapRenderer r : map.getRenderers())
{
map.removeRenderer(r);
}
map.addRenderer(new ImageMapRenderer(image));
return map;
}
private ItemStack getMapItem(MapView map)
{
ItemStack item = new ItemStack(Material.MAP);
item.setDurability(map.getId());
return item;
}
private BufferedImage loadImage(String file)
{
File f = new File(file);
BufferedImage image = null;
try
{
if (!f.exists())
{
image = ImageIO.read(URI.create(file).toURL().openStream());
}
else
{
image = ImageIO.read(f);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
@EventHandler
public void onJoin(PlayerJoinEvent event)
{
for (MapView map : _maps.values())
{
event.getPlayer().sendMap(map);
}
}
public void remove()
{
HandlerList.unregisterAll(this);
for (ItemFrame itemFrame : _maps.keySet())
{
itemFrame.remove();
}
}
private void setItemFrame(Block block, BlockFace face, BufferedImage image)
{
if (!UtilBlock.solid(block))
{
block.setType(Material.QUARTZ_BLOCK);
}
ItemFrame itemFrame = spawnItemFrame(block, face);
MapView map = getMap(block.getWorld(), image);
_maps.put(itemFrame, map);
ItemStack mapItem = getMapItem(map);
itemFrame.setItem(mapItem);
for (Player player : UtilServer.getPlayers())
{
player.sendMap(map);
}
}
private ItemFrame spawnItemFrame(Block block, BlockFace bf)
{
int dir;
switch (bf)
{
default:
case SOUTH:
dir = 0;
break;
case WEST:
dir = 1;
break;
case NORTH:
dir = 2;
break;
case EAST:
dir = 3;
break;
}
WorldServer world = ((CraftWorld) block.getWorld()).getHandle();
EntityItemFrame entity = new EntityItemFrame(world, block.getX(), block.getY(), block.getZ(), dir);
entity.setDirection(dir);
world.addEntity(entity);
return (ItemFrame) entity.getBukkitEntity();
}
private BufferedImage toBufferedImage(Image img, int imageType)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), imageType);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
}

View File

@ -1,21 +0,0 @@
package mineplex.core.map.commands;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.map.Map;
public class MapImage extends CommandBase<Map>
{
public MapImage(Map plugin)
{
super(plugin, Rank.OWNER, new String[] {"mi"});
}
@Override
public void Execute(Player caller, String[] args)
{
Plugin.SpawnMap(caller, args);
}
}

View File

@ -86,7 +86,6 @@ import mineplex.hub.commands.GameModeCommand;
import mineplex.hub.commands.NewsCommand;
import mineplex.hub.modules.ForcefieldManager;
import mineplex.hub.modules.JumpManager;
import mineplex.hub.modules.MapManager;
import mineplex.hub.modules.NewsManager;
import mineplex.hub.modules.ParkourManager;
import mineplex.hub.modules.TextManager;
@ -167,7 +166,6 @@ public class HubManager extends MiniClientPlugin<HubClient>
_textCreator = new TextManager(this);
_parkour = new ParkourManager(this, donationManager, taskManager);
new MapManager(this);
new WorldManager(this);
new JumpManager(this);
new UHCManager(this);

View File

@ -1,61 +0,0 @@
package mineplex.hub.modules;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.entity.ItemFrame;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import mineplex.core.MiniPlugin;
import mineplex.core.map.Map;
import mineplex.core.common.Rank;
import mineplex.core.common.util.UtilGear;
import mineplex.hub.HubManager;
public class MapManager extends MiniPlugin
{
private HubManager Manager;
private Map Map;
public MapManager(HubManager manager)
{
super("Map Manager", manager.GetPlugin());
Map = new Map(manager.GetPlugin());
Manager = manager;
}
//@EventHandler
public void PlayerJoin(PlayerJoinEvent event)
{
try
{
Map.SetDefaultUrl("http://chivebox.com/img/mc/news.png");
event.getPlayer().setItemInHand(Map.GetMap());
}
catch (Exception ex)
{
System.out.println("[MapManager] Player Join exception");
throw ex;
}
}
@EventHandler
public void FrameInteract(PlayerInteractEntityEvent event)
{
if (!(event.getRightClicked() instanceof ItemFrame))
return;
if (!Manager.GetClients().Get(event.getPlayer()).GetRank().Has(Rank.OWNER))
{
event.setCancelled(true);
return;
}
if (!UtilGear.isMat(event.getPlayer().getItemInHand(), Material.DIAMOND_AXE))
return;
event.getRightClicked().getWorld().playEffect(event.getRightClicked().getLocation(), Effect.STEP_SOUND, 5);
event.getRightClicked().remove();
}
}