package me.km.nms; import java.util.List; import me.hammerle.code.Script; import me.hammerle.exceptions.IllegalStringException; import me.km.api.Location; 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.entity.player.EntityPlayerMP; 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.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.ChatType; 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.world.World; import net.minecraft.world.chunk.storage.AnvilChunkLoader; public class NmsUtilities { // ----------------------------------------------------------------------------------- // Entity Tools // ----------------------------------------------------------------------------------- public static List getCollidingEntities(Entity e, double x, double y, double z) { return e.getEntityWorld().getEntitiesWithinAABBExcludingEntity(e, e.getCollisionBoundingBox().expand(x, y, z)); } public static List getCollidingEntities(Entity e) { return getCollidingEntities(e, 0, 0, 0); } public static void walkTo(EntityLiving liv, Vec3d v, double speed) { liv.getMoveHelper().setMoveTo(v.x, v.y, v.z, speed); } public static void canDestroyBlocks(EntityLiving liv) { liv.tasks.addTask(1, new PathfinderGoalDestroyBlock(liv)); } public static String getNbtString(Entity ent) { NBTTagCompound tag = new NBTTagCompound(); ent.writeToNBTAtomically(tag); return tag.toString(); } public static Entity getEntityFromNbtString(Script sc, 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(sc, 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); } // ------------------------------------------------------------------------- // Action-Bar // ------------------------------------------------------------------------- public static void sendActionBar(EntityPlayerMP p, String message) { p.connection.sendPacket(new SPacketChat(new TextComponentString(message), ChatType.GAME_INFO)); } }