package me.km.api; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Random; import java.util.stream.Collectors; import me.km.KajetansMod; import me.km.exception.PlayerNotFoundException; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.block.Block; import net.minecraft.block.BlockButton; import net.minecraft.block.BlockContainer; import net.minecraft.block.BlockDoor; import net.minecraft.block.BlockPistonMoving; import net.minecraft.block.BlockTrapDoor; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemAxe; import net.minecraft.item.ItemHoe; import net.minecraft.item.ItemPickaxe; import net.minecraft.item.ItemSpade; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemSword; public class Utils { // ------------------------------------------------------------------------- // Befehle für Werkzeuge // ------------------------------------------------------------------------- public enum ToolTypes { PICKAXE, AXE, HOE, SHOVEL, SWORD, MUSKET, DAGGER, HAMMER, STICK, WAND, SPEAR } public static ToolTypes getToolType(ItemStack stack) { if(stack == null) { return null; } Item item = stack.getItem(); if(item instanceof ItemHoe) { return ToolTypes.HOE; } else if(item instanceof ItemPickaxe) { return ToolTypes.PICKAXE; } else if(item instanceof ItemAxe) { return ToolTypes.AXE; } else if(item instanceof ItemSpade) { return ToolTypes.SHOVEL; } else if(item instanceof ItemSword) { return ToolTypes.SWORD; } return null; } // ------------------------------------------------------------------------- // Zufallszahlen // ------------------------------------------------------------------------- public static int randomInt(int min, int max) { return new Random().nextInt((max - min) + 1) + min; } public static boolean randomBoolean() { return new Random().nextBoolean(); } // ------------------------------------------------------------------------- // String-Tools // ------------------------------------------------------------------------- public static String formatString(String original) { if(original.length() == 0) { return original; } else if(original.length() == 1) { return original.toUpperCase(); } String[] parts = original.split("_"); for(int i = 0; i < parts.length; i++) { parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase(); } return connectSpaces(parts, 0); } public static String connect(String[] array, int start, String c) { if(start >= array.length) { return ""; } return Arrays.stream(array, start, array.length).collect(Collectors.joining(c)); } public static String connectSpaces(String[] array, int start) { return connect(array, start, " "); } public static String connect(String[] array, int start) { if(start >= array.length) { return ""; } return Arrays.stream(array, start, array.length).collect(Collectors.joining()); } // ------------------------------------------------------------------------- // ItemStack-Tools // ------------------------------------------------------------------------- public static List getLore(ItemStack stack) { //TODO /*if(!stack.hasItemMeta() || !stack.getItemMeta().hasLore()) {*/ return new ArrayList<>(); /*} return new ArrayList<>(stack.getItemMeta().getLore());*/ } public static void setLore(ItemStack stack, String s, int i) { //TODO /*ItemMeta meta = stack.getItemMeta(); List list; if(meta.hasLore()) { list = meta.getLore(); } else { list = new ArrayList<>(); } if(i >= list.size()) { list.add(s); } else { list.set(i, s); } meta.setLore(list); stack.setItemMeta(meta);*/ } public static ArrayList buildLimitedLore(String whole, int limit, String addition) { ArrayList list = new ArrayList<>(); int pos = 0; int space; while(pos + limit < whole.length()) { space = whole.lastIndexOf(" ", pos + limit); if(space == -1 || pos > space) { space = whole.indexOf(" ", pos + limit); } list.add(addition + whole.substring(pos, space)); pos = space + 1; } list.add(addition + whole.substring(pos)); return list; } public static ArrayList buildLimitedLore(String whole, String addition) { return buildLimitedLore(whole, 25, addition); } public static void dropRandomTreeItem(World w, BlockPos l, IBlockState b) { Block block = b.getBlock(); int identifier = block.getMetaFromState(b) % 4; if(b == Blocks.LEAVES2) { identifier += 4; } ArrayList list = new ArrayList<>(); list.add(Items.NAME_TAG); list.add(Items.LEAD); list.add(Blocks.WEB); if(identifier == 3) { list.add(Blocks.VINE); list.add(Items.DYE); list.add(Blocks.VINE); list.add(Items.DYE); list.add(Blocks.VINE); list.add(Items.DYE); } if(identifier == 0 || identifier == 5) { list.add(Items.APPLE); list.add(Items.APPLE); list.add(Items.APPLE); list.add(Items.APPLE); list.add(Items.GOLDEN_APPLE); } list.add(Items.STICK); list.add(Items.STICK); list.add(Items.STICK); list.add(Items.STICK); list.add(Items.STRING); list.add(Items.STRING); list.add(Items.FEATHER); list.add(Items.FEATHER); if(identifier == 4) { list.add(Items.BONE); list.add(Items.BONE); list.add(Items.BONE); } int rand = Utils.randomInt(0, 19); if(rand >= list.size()) { return; } if(list.get(rand) == Items.DYE) { new ItemStackBuilder(Items.DYE, 1, (short) 3).drop(w, l); return; } if(list.get(rand) instanceof Item) { new ItemStackBuilder((Item) list.get(rand), 1).drop(w, l); return; } new ItemStackBuilder((Block) list.get(rand), 1).drop(w, l); } // ------------------------------------------------------------------------- // Inventory-Tools // ------------------------------------------------------------------------- /*public static int getLowestStackAmount(CraftingInventory inv) { return Arrays.stream(inv.getMatrix()).filter(s -> s != null && s.getType() != Material.AIR).mapToInt(s -> s.getAmount()).min().orElse(0); } public static Integer searchInventoryFor(Inventory inv, ItemStack searchfor, boolean ignoreDv) { if(!ignoreDv) { return Arrays.stream(inv.getContents()).filter(s -> s != null && s.isSimilar(searchfor)).mapToInt(s -> s.getAmount()).sum(); } Material m = searchfor.getType(); return Arrays.stream(inv.getContents()).filter(s -> s != null && s.getType() == m).mapToInt(s -> s.getAmount()).sum(); }*/ // ------------------------------------------------------------------------- // Entities // ------------------------------------------------------------------------- public static Location getEntityLocation(Entity ent) { return new Location(ent.getEntityWorld(), ent.getPositionVector(), ent.rotationYaw, ent.rotationPitch); } public static BlockPos getEyeLocation(Entity ent) { return ent.getPosition().add(0, ent.getEyeHeight(), 0); } public static void teleportEntity(Entity ent, Location l) { Vec3d pos = l.getPos(); if(ent instanceof EntityPlayerMP) { if(l.getYaw() != 0 && l.getPitch() != 0) { ((EntityPlayerMP) ent).connection.setPlayerLocation(pos.xCoord, pos.yCoord, pos.zCoord, l.getYaw(), l.getPitch()); } else { ((EntityPlayerMP) ent).connection.setPlayerLocation(pos.xCoord, pos.yCoord, pos.zCoord, ent.rotationYaw, ent.rotationPitch); } } else { if(l.getYaw() != 0 && l.getPitch() != 0) { ent.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, l.getYaw(), l.getPitch()); } else { ent.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, ent.rotationYaw, ent.rotationPitch); } } } public static void teleportEntity(Entity ent, Entity ent2) { teleportEntity(ent, new Location(ent2.world, ent.getPositionVector(), ent2.rotationYaw, ent2.rotationPitch)); } // ------------------------------------------------------------------------- // Entities aus Umgebung // ------------------------------------------------------------------------- @SuppressWarnings("unchecked") public static List getEntitiesExcluding(Entity ent, World w, BlockPos pos, BlockPos pos2) { return w.getEntitiesWithinAABBExcludingEntity(ent, new AxisAlignedBB(pos, pos2)); } @SuppressWarnings("unchecked") public static Collection getNearbyEntities(World w, BlockPos l, double radius, Class type) { double sqareRadius = radius * radius; return w.getEntitiesWithinAABB(type, new AxisAlignedBB( l.getX() - radius, l.getY() - radius, l.getZ() - radius, l.getX() + radius, l.getY() + radius, l.getZ() + radius), ent -> ent.getDistanceSq(l) <= sqareRadius); } public static T getNearestEntity(World w, BlockPos l, double radius, Class type) { return getNearbyEntities(w, l, radius, type).stream().min((e1, e2) -> Double.compare( e1.getDistanceSq(l), e2.getDistanceSq(l))) .orElse(null); } @SuppressWarnings("unchecked") public static T getTargetedEntity(EntityPlayer p, int radius, Class type) { World w = p.getEntityWorld(); BlockPos l = getPlayerTarget(p, radius); BlockPos eye = getEyeLocation(p); List col = getEntitiesExcluding(p, w, eye, l); col.removeIf(ent -> !type.isAssignableFrom(ent.getClass())); // Entfernt Entities, die sich nicht mit dem Blick-Vektor schneiden Vec3d first = new Vec3d(eye.getX(), eye.getY(), eye.getZ()); Vec3d second = new Vec3d(l.getX(), l.getY(), l.getZ()); col.removeIf(ent -> { AxisAlignedBB bound = ent.getCollisionBoundingBox(); if(bound == null) { return true; } return bound.calculateIntercept(first, second) == null; }); return (T) col.stream().sorted((Entity e1, Entity e2) -> { return Double.compare(e1.getDistanceSq(eye), e2.getDistanceSq(eye)); }).findFirst().orElse(null); } // ------------------------------------------------------------------------- // Player-Tools // ------------------------------------------------------------------------- public static EntityPlayer getNearestPlayer(World w, BlockPos l) { return w.getPlayers(EntityPlayer.class, p -> true).stream().min((p1, p2) -> Double.compare(p1.getDistanceSq(l), p2.getDistanceSq(l))).orElse(null); } public static BlockPos getPlayerTarget(EntityPlayer p, int range) { if(range > 64) { range = 64; } World w = p.getEntityWorld(); BlockPos l = getEyeLocation(p); Vec3d unit2 = p.getLookVec(); Vec3i unit = new Vec3i(unit2.xCoord, unit2.yCoord, unit2.zCoord); for(int i = 0; i < range; i++) { l = l.add(unit); if(w.isBlockFullCube(l)) { break; } } return l; } public static BlockPos getPlayerTarget(EntityPlayer p) { return getPlayerTarget(p, 20); } public static EntityPlayerMP getPlayerByName(String name) throws PlayerNotFoundException { return KajetansMod.server.getPlayerList().getPlayers().stream() .filter(pl -> pl.getName().contains(name)) .findFirst().orElseThrow(() -> new PlayerNotFoundException(name)); } // ------------------------------------------------------------------------- // Spawn // ------------------------------------------------------------------------- public static Location getSpawn() { // Player changeDimension, WorldServer, MinecraftServer return KajetansMod.conf.getLocation("spawn"); } public static void setSpawn(World w, Vec3d v, float yaw, float pitch) { SimpleConfig conf = KajetansMod.conf; conf.setLocation("spawn", new Location(w, v, yaw, pitch)); conf.save(); w.setSpawnPoint(new BlockPos(v.xCoord, v.yCoord, v.zCoord)); } // ------------------------------------------------------------------------- // Warps // ------------------------------------------------------------------------- public static Location getWarp(Module m, String name) { SimpleConfig conf = new SimpleConfig(m, "warp/" + name, true); if(conf.exists()) { return conf.getLocation("loc"); } return null; } // ------------------------------------------------------------------------- // Blocks // ------------------------------------------------------------------------- public static Block getSameNearbyBlock(World w, BlockPos pos, Block b) { Block bcheck = Location.getRelativeBlock(w, pos, 1, 0, 0).getBlock(); if(bcheck == b) { return bcheck; } bcheck = Location.getRelativeBlock(w, pos, -1, 0, 0).getBlock(); if(bcheck == b) { return bcheck; } bcheck = Location.getRelativeBlock(w, pos, 0, 0, 1).getBlock();; if(bcheck == b) { return bcheck; } bcheck = Location.getRelativeBlock(w, pos, 0, 0, -1).getBlock(); if(bcheck == b) { return bcheck; } return null; } public static boolean shouldBeProtected(Block b) { return b instanceof BlockDoor || !(b instanceof BlockPistonMoving) || b instanceof BlockContainer || b == Blocks.LEVER || b instanceof BlockButton || b instanceof BlockTrapDoor; } // ------------------------------------------------------------------------- // Roman Numbers // ------------------------------------------------------------------------- public static String intToRoman(int i) { switch(i) { case 1: return "I"; case 2: return "II"; case 3: return "III"; case 4: return "IV"; case 5: return "V"; case 6: return "VI"; case 7: return "VII"; case 8: return "VIII"; case 9: return "IX"; case 10: return "X"; case 11: return "XI"; case 12: return "XII"; case 13: return "XIII"; case 14: return "XIV"; case 15: return "XV"; case 16: return "XVI"; case 17: return "XVII"; case 18: return "XVIII"; case 19: return "XIX"; case 20: return "XX"; } return "I"; } public static int romanToInt(String s) { switch(s) { case "I": return 1; case "II": return 2; case "III": return 3; case "IV": return 4; case "V": return 5; case "VI": return 6; case "VII": return 7; case "VIII": return 8; case "IX": return 9; case "X": return 10; case "XI": return 11; case "XII": return 12; case "XIII": return 13; case "XIV": return 14; case "XV": return 15; case "XVI": return 16; case "XVII": return 17; case "XVIII": return 18; case "XIX": return 19; case "XX": return 20; } return 1; } }