Clean up billboard generation and clearing, rewrite MapRender for logos, and add JavaDocs to comply with code standard

This commit is contained in:
AlexTheCoder 2016-05-01 14:58:40 -04:00
parent 48c8a5a630
commit 335c5a4510
3 changed files with 78 additions and 32 deletions

View File

@ -13,6 +13,10 @@ import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.plugin.java.JavaPlugin;
/**
*
* Manager for creating billboards with branding logos
*/
public class BrandingManager extends MiniPlugin
{
private ConcurrentHashMap<Integer, BrandingPost> _posts = new ConcurrentHashMap<Integer, BrandingPost>();
@ -52,6 +56,12 @@ public class BrandingManager extends MiniPlugin
return image;
}
/**
* Generates a billboard with a stored logo
* @param location The center of the billboard
* @param facing The BlockFace the logo will appear on
* @param imageFileName The file name of the logo's base image
*/
public void createPost(Location location, BlockFace facing, String imageFileName)
{
BufferedImage img = getImage(imageFileName);
@ -65,6 +75,9 @@ public class BrandingManager extends MiniPlugin
_posts.put(_posts.size(), bp);
}
/**
* Clears away all existing billboards
*/
public void reset()
{
disable();

View File

@ -7,6 +7,7 @@ import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.ItemFrame;
import org.bukkit.inventory.ItemStack;
@ -15,6 +16,9 @@ import org.bukkit.map.MapView;
import com.google.common.collect.Lists;
/**
* An instance of a billboard with all its required data
*/
public class BrandingPost
{
private Location _center;
@ -43,12 +47,14 @@ public class BrandingPost
}
map.addRenderer(new LogoMapRenderer(image, x, y));
item.setDurability(map.getId());
return item;
}
/**
* Generates a billboard using parameters from instance constructor
*/
@SuppressWarnings("deprecation")
public void spawn()
{
@ -106,30 +112,61 @@ public class BrandingPost
}
}
for (int x = Math.max(corner1.getBlockX(), corner2.getBlockX()); x >= Math.min(corner1.getBlockX(), corner2.getBlockX()); x--)
int xMod = 0;
int zMod = 0;
switch (_facing)
{
int mapX = 0;
if (mapX + 1 < width)
case EAST:
zMod = -1;
break;
case WEST:
zMod = 1;
break;
case SOUTH:
xMod = 1;
break;
case NORTH:
xMod = -1;
break;
default:
break;
}
BufferedImage image = _img;
if (image == null)
{
System.out.println("ERROR! Invalid image given while loading BrandingPost!");
return;
}
Block base = corner1.getBlock().getRelative(_facing);
try
{
for (int x = 0; x < width; x++)
{
mapX++;
}
for (int y = Math.max(corner1.getBlockY(), corner2.getBlockY()); y >= Math.min(corner1.getBlockY(), corner2.getBlockY()); y--)
{
int mapY = 0;
if (mapY + 1 < height)
for (int y = 0; y < height; y++)
{
mapY++;
}
for (int z = Math.max(corner1.getBlockZ(), corner2.getBlockZ()); z >= Math.min(corner1.getBlockZ(), corner2.getBlockZ()); z--)
{
Location set = new Location(_center.getWorld(), x, y, z);
ItemFrame ent = set.getWorld().spawn(set.getBlock().getRelative(_facing).getLocation(), ItemFrame.class);
ent.setFacingDirection(_facing, false);
ItemStack map = getMapItem((mapX) * 128, mapY * 128, _img);
ent.setItem(map);
ItemFrame i = null;
Block block = base.getRelative(x * xMod, -y, x * zMod);
i = block.getWorld().spawn(block.getLocation(), ItemFrame.class);
i.setFacingDirection(_facing, false);
ItemStack item = getMapItem(x, y, _img);
i.setItem(item);
_ents.add(i);
}
}
}
catch (NullPointerException e)
{
Bukkit.broadcastMessage("ERROR! ItemFrame space already occupied!");
return;
}
for (int x = Math.max(corner3.getBlockX(), corner4.getBlockX()); x >= Math.min(corner3.getBlockX(), corner4.getBlockX()); x--)
{
@ -148,6 +185,9 @@ public class BrandingPost
}
}
/**
* Clears away all blocks and ItemFrames generated by this billboard
*/
public void despawn()
{
if (_corners != null)
@ -166,6 +206,7 @@ public class BrandingPost
}
for (ItemFrame it : _ents)
{
it.setItem(new ItemStack(Material.AIR));
it.remove();
}
_ents.clear();

View File

@ -20,25 +20,17 @@ public class LogoMapRenderer extends MapRenderer
public void recalculateInput(BufferedImage input, int x1, int y1)
{
int x2 = 128;
int y2 = 128;
if (x1 > input.getWidth() || y1 > input.getHeight())
{
return;
}
if (x1 + x2 >= input.getWidth())
{
x2 = input.getWidth() - x1;
}
int startX = Math.min(x1 * 128, input.getWidth());
startX = Math.max(input.getMinX(), startX);
int startY = Math.min(y1 * 128, input.getHeight());
startY = Math.max(input.getMinY(), startY);
if (y1 + y2 >= input.getHeight())
{
y2 = input.getHeight() - y1;
}
_img = input.getSubimage(x1, y1, x2, y2);
_img = input.getSubimage(startX, startY, 128, 128);
_first = true;
}