123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package me.km.utils;
- import me.hammerle.snuviscript.code.InputProvider;
- import me.hammerle.snuviscript.code.Script;
- import me.km.api.Location;
- import me.km.exception.IllegalStringException;
- import net.minecraft.block.Block;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.nbt.JsonToNBT;
- import net.minecraft.nbt.NBTException;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3d;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.Style;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraft.util.text.event.ClickEvent;
- import net.minecraft.util.text.event.HoverEvent;
- import net.minecraft.world.World;
- import net.minecraft.world.chunk.storage.AnvilChunkLoader;
- public class NBTUtils
- {
- // -----------------------------------------------------------------------------------
- // Entity Tools
- // -----------------------------------------------------------------------------------
- public static String toString(Entity ent)
- {
- NBTTagCompound tag = new NBTTagCompound();
- ent.writeToNBTAtomically(tag);
- return tag.toString();
- }
-
- public static Entity fromString(String s, Location l)
- {
- try
- {
- NBTTagCompound nbt = JsonToNBT.getTagFromJson(s);
- Vec3d v = l.getPos();
- Entity ent = AnvilChunkLoader.readWorldEntityPos(nbt, l.getWorld(), v.x, v.y, v.z, true);
- if(ent == null)
- {
- return null;
- }
- else
- {
- ent.setLocationAndAngles(v.x, v.y, v.z, ent.rotationYaw, ent.rotationPitch);
- if(ent instanceof EntityLiving)
- {
- ((EntityLiving) ent).onInitialSpawn(ent.getEntityWorld().getDifficultyForLocation(new BlockPos(ent)), null);
- }
- }
- return ent;
- }
- catch(NBTException ex)
- {
- throw new IllegalStringException("illegal entity string '" + s + "'");
- }
- }
-
- // -------------------------------------------------------------------------
- // Block Tools
- // -------------------------------------------------------------------------
-
- @SuppressWarnings(value = {"unchecked", "deprecation"})
- public static void setBlockWithData(World w, BlockPos pos, int dv, Block block, String data)
- {
- NBTTagCompound nbttagcompound = new NBTTagCompound();
- boolean flag = false;
- if (data != null && block.hasTileEntity())
- {
- try
- {
- nbttagcompound = JsonToNBT.getTagFromJson(data);
- flag = true;
- }
- catch(NBTException ex)
- {
- throw new IllegalStringException(data);
- }
- }
- TileEntity tileentity = w.getTileEntity(pos);
- if (tileentity != null)
- {
- if (tileentity instanceof IInventory)
- {
- ((IInventory) tileentity).clear();
- }
- w.setBlockState(pos, Blocks.AIR.getDefaultState(), block == Blocks.AIR ? 2 : 4);
- }
- IBlockState iblockdata = block.getStateFromMeta(dv);
- if(w.setBlockState(pos, iblockdata, 2))
- {
- if(flag)
- {
- TileEntity tileentity1 = w.getTileEntity(pos);
- if (tileentity1 != null)
- {
- nbttagcompound.setInteger("x", pos.getX());
- nbttagcompound.setInteger("y", pos.getY());
- nbttagcompound.setInteger("z", pos.getZ());
- tileentity1.readFromNBT(nbttagcompound);
- }
- }
- w.notifyNeighborsRespectDebug(pos, iblockdata.getBlock(), false);
- }
- }
- // -------------------------------------------------------------------------
- // NBT Tools
- // -------------------------------------------------------------------------
-
- public static String highlightNbtSyntax(String text)
- {
- StringBuilder sb = new StringBuilder(text);
- int counter = 0;
- for(char c : text.toCharArray())
- {
- switch(c)
- {
- case '}':
- sb.replace(counter, counter + 1, "§c} §r");
- counter += 6;
- continue;
- case '{':
- sb.replace(counter, counter + 1, "§c{§r");
- counter += 5;
- continue;
- case ']':
- sb.replace(counter, counter + 1, "§c] §r");
- counter += 6;
- continue;
- case '[':
- sb.replace(counter, counter + 1, "§c[§r");
- counter += 5;
- continue;
- case ',':
- sb.replace(counter, counter + 1, "§6, §r");
- counter += 6;
- continue;
- case '"':
- sb.replace(counter, counter + 1, "§a\"");
- counter += 3;
- continue;
- case ':':
- sb.replace(counter, counter + 1, "§8:");
- counter += 3;
- continue;
- case '0':
- sb.replace(counter, counter + 1, "§50");
- counter += 3;
- continue;
- case '1':
- sb.replace(counter, counter + 1, "§51");
- counter += 3;
- continue;
- case '2':
- sb.replace(counter, counter + 1, "§52");
- counter += 3;
- continue;
- case '3':
- sb.replace(counter, counter + 1, "§53");
- counter += 3;
- continue;
- case '4':
- sb.replace(counter, counter + 1, "§54");
- counter += 3;
- continue;
- case '5':
- sb.replace(counter, counter + 1, "§55");
- counter += 3;
- continue;
- case '6':
- sb.replace(counter, counter + 1, "§56");
- counter += 3;
- continue;
- case '7':
- sb.replace(counter, counter + 1, "§57");
- counter += 3;
- continue;
- case '8':
- sb.replace(counter, counter + 1, "§58");
- counter += 3;
- continue;
- case '9':
- sb.replace(counter, counter + 1, "§59");
- counter += 3;
- continue;
- default:
- counter++;
- }
- }
- return sb.toString();
- }
-
- // -------------------------------------------------------------------------
- // Copy Tools
- // -------------------------------------------------------------------------
-
- public static void sendCopyableText(EntityPlayer p, String s)
- {
- s = s.replace('{', 'Ɛ');
- s = s.replace('}', 'Ƒ');
- s = s.replace('[', 'ƒ');
- s = s.replace(']', 'Ɠ');
- s = s.replace('(', 'ƕ');
- s = s.replace(')', 'Ɩ');
- s = s.replace(" ", "%20");
- s = s.replace('\'', 'Ɩ');
- s = s.replace('"', 'Ɩ');
- s = s.replace("\\", "");
- sendLink(p, "Hier drücken zum Kopieren.", "http://minecraft.hammerle.me/showtext.php/?text=" + s);
- }
-
- private static void sendLink(EntityPlayer p, String s, String link)
- {
- TextComponentString text = new TextComponentString(s);
- Style style = text.getStyle();
- style.setColor(TextFormatting.RED);
- style.setBold(true);
- style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link));
- p.sendMessage(text);
- }
-
- public static ITextComponent getClickable(Object message, String command)
- {
- ITextComponent text;
- if(message instanceof ITextComponent)
- {
- text = (ITextComponent) message;
- }
- else
- {
- text = new TextComponentString(String.valueOf(message));
- }
- Style style = text.getStyle();
- style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command));
- return text;
- }
-
- public static ITextComponent getHoverable(Object message, String hover)
- {
- ITextComponent text;
- if(message instanceof ITextComponent)
- {
- text = (ITextComponent) message;
- }
- else
- {
- text = new TextComponentString(String.valueOf(message));
- }
- Style style = text.getStyle();
- style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(hover)));
- return text;
- }
-
- public static ITextComponent concat(Script sc, int start, String pre, InputProvider... ob)
- {
- TextComponentString text = new TextComponentString(pre);
- Object o;
- for(int i = start; i < ob.length; i++)
- {
- o = ob[i].get(sc);
- if(o instanceof ITextComponent)
- {
- text.appendSibling((ITextComponent) o);
- }
- else
- {
- text.appendText(String.valueOf(o));
- }
- }
- return text;
- }
-
- public static ITextComponent concat(Script sc, int start, InputProvider... ob)
- {
- return concat(sc, start, "", ob);
- }
- }
|