123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- package me.km.nms;
- import java.util.List;
- import me.km.api.Location;
- import me.km.exception.IllegalItemStackStringException;
- 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.ai.attributes.AttributeModifier;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.init.Blocks;
- import net.minecraft.inventory.EntityEquipmentSlot;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.JsonToNBT;
- import net.minecraft.nbt.NBTException;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.network.play.server.SPacketChat;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3d;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.world.World;
- import net.minecraft.world.chunk.storage.AnvilChunkLoader;
- public class NmsUtilities
- {
- // -----------------------------------------------------------------------------------
- // Entity Tools
- // -----------------------------------------------------------------------------------
-
- public static List<Entity> getCollidingEntities(Entity e, double x, double y, double z)
- {
- return e.getEntityWorld().getEntitiesWithinAABBExcludingEntity(e, e.getCollisionBoundingBox().expand(x, y, z));
- }
-
- public static List<Entity> getCollidingEntities(Entity e)
- {
- return getCollidingEntities(e, 0, 0, 0);
- }
- /*public static void walkTo(org.bukkit.entity.Entity e, org.bukkit.Location l, double speed, double radius)
- {
- EntityCreature el = ((CraftCreature) e).getHandle();
- el.goalSelector.a(1, new PathfinderGoalWalkToLocation(el, l, speed, radius));
- }
-
- public static void canDestroyBlocks(org.bukkit.entity.LivingEntity e)
- {
- EntityCreature el = ((CraftCreature) e).getHandle();
- el.goalSelector.a(1, new PathfinderGoalDestroyBlock(el));
- }*/
-
- public static String getNbtString(Entity ent)
- {
- NBTTagCompound tag = new NBTTagCompound();
- ent.writeToNBTAtomically(tag);
- return tag.toString();
- }
-
- public static Entity getEntityFromNbtString(String s, Location l)
- {
- try
- {
- NBTTagCompound nbt = JsonToNBT.getTagFromJson(s);
- Vec3d v = l.getPos();
- Entity ent = AnvilChunkLoader.readWorldEntityPos(nbt, l.getWorld(), v.xCoord, v.yCoord, v.zCoord, true);
- if(ent == null)
- {
- return null;
- }
- else
- {
- ent.setLocationAndAngles(v.xCoord, v.yCoord, v.zCoord, 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(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);
- }
- }
-
- // -------------------------------------------------------------------------
- // ItemStack Tools
- // -------------------------------------------------------------------------
-
- public enum Attribute
- {
- /**
- * Wert von 0 bis 30;
- */
- ARMOR("generic.armor"),
- /**
- * Wert von 0 bis 20;
- */
- ARMOR_TOUGHNESS("generic.armorToughness"),
- /**
- * Wert von 0 bis 1.7E308;
- */
- ATTACK_DAMAGE("generic.attackDamage"),
- /**
- * Wert von 0 bis 1;
- */
- KNOCKBACK_RESISTANCE("generic.knockbackResistance"),
- /**
- * Wert von 0 bis 1.7E308;
- */
- MAX_HEALTH("generic.maxHealth"),
- /**
- * Wert von 0 bis 1.7E308;
- */
- MOVEMENT_SPEED("generic.movementSpeed"),
- /**
- * Wert von 0 bis 1024;
- */
- ATTACK_SPEED("generic.attackSpeed"),
- /**
- * Wert von -1024 bis 1024;
- */
- LUCK("generic.luck");
-
- private final String name;
-
- Attribute(String name)
- {
- this.name = name;
- }
-
- public String getName()
- {
- return name;
- }
- }
-
- public enum Operation
- {
- ADD, MUL, MUL_CHANGED
- }
-
- public static void addAttribute(ItemStack stack, Attribute a, EntityEquipmentSlot slot, double amount, Operation op)
- {
- stack.addAttributeModifier(a.getName(), new AttributeModifier("modifier", amount, op.ordinal()), slot);
- }
-
- public static String getNbtString(ItemStack stack)
- {
- if(stack == null)
- {
- return "null";
- }
- return stack.writeToNBT(new NBTTagCompound()).toString();
- }
-
- public static ItemStack getStackFromNbtString(String s) throws IllegalItemStackStringException
- {
- try
- {
- NBTTagCompound c = JsonToNBT.getTagFromJson(s);
- return new ItemStack(c);
- }
- catch(NBTException ex)
- {
- throw new IllegalItemStackStringException(s);
- }
- }
-
- // -------------------------------------------------------------------------
- // 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(EntityPlayerMP p, String s)
- {
- s = s.replaceAll("\\{", "(");
- s = s.replaceAll("\\}", ")");
- s = s.replaceAll(" ", "%20");
- s = s.replaceAll("\"", "'");
- SPacketChat packet = new SPacketChat(new TextComponentString(
- "[{\"text\":\"Hier drücken zum Kopieren.\",\"color\":\"red\",\"bold\":\"true\",\"clickEvent\":" +
- "{\"action\":\"open_url\",\"value\":\"http://ts.hammerle.me/showtext.php/?text=" + s + "\"}}]"), (byte) 0);
- p.connection.sendPacket(packet);
- }
-
- // -------------------------------------------------------------------------
- // Action-Bar
- // -------------------------------------------------------------------------
-
- public static void sendActionBar(EntityPlayerMP p, String message)
- {
- p.connection.sendPacket(new SPacketChat(new TextComponentString(message), (byte) 2));
- }
- }
|