123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- 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<String> 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<String> 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<String> buildLimitedLore(String whole, int limit, String addition)
- {
- ArrayList<String> 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<String> 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<Object> 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<Entity> getEntitiesExcluding(Entity ent, World w, BlockPos pos, BlockPos pos2)
- {
- return w.getEntitiesWithinAABBExcludingEntity(ent, new AxisAlignedBB(pos, pos2));
- }
-
- @SuppressWarnings("unchecked")
- public static <T extends Entity> Collection<T> getNearbyEntities(World w, BlockPos l, double radius, Class<T> 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 extends Entity> T getNearestEntity(World w, BlockPos l, double radius, Class<T> 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 extends Entity> T getTargetedEntity(EntityPlayer p, int radius, Class<T> type)
- {
- World w = p.getEntityWorld();
- BlockPos l = getPlayerTarget(p, radius);
- BlockPos eye = getEyeLocation(p);
- List<Entity> 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;
- }
- }
|