Fix icon spacing & unhandled NPEs

This commit is contained in:
filoghost 2014-09-01 20:34:38 +02:00
parent 3bc0d31d85
commit 4718ff3db1
8 changed files with 233 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.gmail.filoghost.holograms.exception.CommandException;
import com.gmail.filoghost.holograms.utils.ItemUtils;
public class CommandValidator {
@ -52,8 +53,8 @@ public class CommandValidator {
@SuppressWarnings("deprecation")
public static ItemStack matchItemStack(String input) throws CommandException {
String copy = input;
input = input.replace(" ", ""); // Cut the spaces
int dataValue = 0;
if (input.contains(":")) {
@ -72,17 +73,11 @@ public class CommandValidator {
}
}
} else {
input = input.replace("_", "").replace("-", "").toLowerCase();
for (Material mat : Material.values()) {
if (mat.toString().replace("_", "").toLowerCase().equals(input)) {
match = mat;
break;
}
}
match = ItemUtils.matchMaterial(input);
}
if (match == null) {
throw new CommandException("Invalid material: " + copy);
if (match == null || match == Material.AIR) {
throw new CommandException("Invalid material: " + input);
}
return new ItemStack(match, 1, (short) dataValue);

View File

@ -1,5 +1,6 @@
package com.gmail.filoghost.holograms.nms.v1_6_R3;
import net.minecraft.server.v1_6_R3.Block;
import net.minecraft.server.v1_6_R3.EntityItem;
import net.minecraft.server.v1_6_R3.ItemStack;
import net.minecraft.server.v1_6_R3.NBTTagCompound;
@ -146,6 +147,10 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) {
ItemStack newItem = CraftItemStack.asNMSCopy(stack);
if (newItem == null) {
newItem = new ItemStack(Block.BEDROCK);
}
if (newItem.tag == null) {
newItem.tag = new NBTTagCompound();
}

View File

@ -19,6 +19,7 @@ import net.minecraft.server.v1_7_R1.EntityHuman;
import net.minecraft.server.v1_7_R1.EntityPlayer;
import net.minecraft.server.v1_7_R1.NBTTagList;
import net.minecraft.server.v1_7_R1.NBTTagString;
import net.minecraft.server.v1_7_R1.Blocks;
public class EntityCustomItem extends EntityItem implements CustomItem, BasicEntityNMS {
@ -146,6 +147,10 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) {
ItemStack newItem = CraftItemStack.asNMSCopy(stack);
if (newItem == null) {
newItem = new ItemStack(Blocks.BEDROCK);
}
if (newItem.tag == null) {
newItem.tag = new NBTTagCompound();
}

View File

@ -19,6 +19,7 @@ import net.minecraft.server.v1_7_R2.EntityHuman;
import net.minecraft.server.v1_7_R2.EntityPlayer;
import net.minecraft.server.v1_7_R2.NBTTagList;
import net.minecraft.server.v1_7_R2.NBTTagString;
import net.minecraft.server.v1_7_R2.Blocks;
public class EntityCustomItem extends EntityItem implements CustomItem, BasicEntityNMS {
@ -146,6 +147,10 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) {
ItemStack newItem = CraftItemStack.asNMSCopy(stack);
if (newItem == null) {
newItem = new ItemStack(Blocks.BEDROCK);
}
if (newItem.tag == null) {
newItem.tag = new NBTTagCompound();
}

View File

@ -11,6 +11,7 @@ import com.gmail.filoghost.holograms.nms.interfaces.CustomItem;
import com.gmail.filoghost.holograms.object.HologramBase;
import com.gmail.filoghost.holograms.utils.ItemUtils;
import net.minecraft.server.v1_7_R3.Blocks;
import net.minecraft.server.v1_7_R3.NBTTagList;
import net.minecraft.server.v1_7_R3.EntityItem;
import net.minecraft.server.v1_7_R3.ItemStack;
@ -145,6 +146,10 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) {
ItemStack newItem = CraftItemStack.asNMSCopy(stack);
if (newItem == null) {
newItem = new ItemStack(Blocks.BEDROCK);
}
if (newItem.tag == null) {
newItem.tag = new NBTTagCompound();
}

View File

@ -1,5 +1,6 @@
package com.gmail.filoghost.holograms.nms.v1_7_R4;
import net.minecraft.server.v1_7_R4.Blocks;
import net.minecraft.server.v1_7_R4.NBTTagList;
import net.minecraft.server.v1_7_R4.NBTTagString;
import net.minecraft.server.v1_7_R4.EntityHuman;
@ -146,6 +147,10 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) {
ItemStack newItem = CraftItemStack.asNMSCopy(stack);
if (newItem == null) {
newItem = new ItemStack(Blocks.BEDROCK);
}
if (newItem.tag == null) {
newItem.tag = new NBTTagCompound();
}

View File

@ -18,6 +18,7 @@ import com.gmail.filoghost.holograms.object.pieces.FloatingDoubleEntity;
import com.gmail.filoghost.holograms.object.pieces.FloatingItemDoubleEntity;
import com.gmail.filoghost.holograms.object.pieces.FloatingTouchSlimeDoubleEntity;
import com.gmail.filoghost.holograms.object.pieces.HologramLine;
import com.gmail.filoghost.holograms.utils.ItemUtils;
import com.gmail.filoghost.holograms.utils.Validator;
/**
@ -139,15 +140,19 @@ public class CraftHologram extends HologramBase implements Hologram {
// It's a floating icon!
ItemStack icon;
try {
icon = CommandValidator.matchItemStack(text.substring(5).trim());
icon = CommandValidator.matchItemStack(text.substring(5));
} catch (CommandException e) {
icon = new ItemStack(Material.BEDROCK);
}
// If the current Y has been changed, the item is NOT on top of the hologram.
if (currentY != this.y) {
// Extra space for the floating item...
currentY -= 0.52;
// Extra space for the floating item, blocks are smaller
if (ItemUtils.appearsAsBlock(icon.getType())) {
currentY -= 0.27;
} else {
currentY -= 0.52;
}
}
FloatingItemDoubleEntity lineEntity = new FloatingItemDoubleEntity(icon);
@ -231,8 +236,12 @@ public class CraftHologram extends HologramBase implements Hologram {
if (lineEntity instanceof FloatingItemDoubleEntity) {
if (currentY != loc.getY()) {
// Extra space for the floating item...
currentY -= 0.52;
// Extra space for the floating item, blocks are smaller
if (ItemUtils.appearsAsBlock(((FloatingItemDoubleEntity) lineEntity).getItemStack().getType())) {
currentY -= 0.27;
} else {
currentY -= 0.52;
}
}
lineEntity.teleport(loc.getX(), currentY, loc.getZ());

View File

@ -1,6 +1,9 @@
package com.gmail.filoghost.holograms.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -37,5 +40,190 @@ public class ItemUtils {
item.setItemMeta(meta);
return item;
}
// A map with formatter materials (lowercase and without dashes) for fast access.
private static Map<String, Material> materialMap = new HashMap<String, Material>();
private static Pattern stripSymbolsPattern = Pattern.compile("_- ");
static {
for (Material mat : Material.values()) {
materialMap.put(mat.toString().toLowerCase().replace("_", ""), mat);
}
}
public static Material matchMaterial(String input) {
return materialMap.get(stripSymbolsPattern.matcher(input.toLowerCase()).replaceAll(""));
}
@SuppressWarnings("deprecation")
public static boolean appearsAsBlock(Material mat) {
switch (mat.getId()) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 7:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 29:
case 33:
case 34:
case 35:
case 36:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 49:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
case 58:
case 59:
case 63:
case 64:
case 67:
case 68:
case 70:
case 71:
case 72:
case 73:
case 74:
case 75:
case 77:
case 78:
case 79:
case 80:
case 81:
case 82:
case 83:
case 84:
case 85:
case 86:
case 87:
case 88:
case 89:
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
case 100:
case 103:
case 104:
case 105:
case 107:
case 108:
case 109:
case 110:
case 112:
case 113:
case 114:
case 115:
case 116:
case 117:
case 118:
case 120:
case 121:
case 122:
case 123:
case 124:
case 125:
case 126:
case 128:
case 129:
case 130:
case 132:
case 133:
case 134:
case 135:
case 136:
case 137:
case 138:
case 139:
case 140:
case 143:
case 144:
case 145:
case 146:
case 147:
case 148:
case 149:
case 150:
case 151:
case 152:
case 153:
case 155:
case 156:
case 157:
case 158:
case 159:
case 161:
case 162:
case 163:
case 164:
case 165:
case 167:
case 168:
case 169:
case 170:
case 171:
case 172:
case 173:
case 174:
case 176:
case 177:
case 178:
case 179:
case 180:
case 181:
case 182:
case 183:
case 184:
case 185:
case 186:
case 187:
case 188:
case 189:
case 190:
case 191:
case 192:
case 193:
case 194:
case 195:
case 196:
case 197:
return true;
default:
return false;
}
}
}