Advertisement
jadenquinn

java04jhb4h43wress

Sep 28th, 2018
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.15 KB | None | 0 0
  1. /*------------------------------------------------------------------------------
  2.  Copyright (c) CovertJaguar, 2011-2016
  3.  
  4.  This work (the API) is licensed under the "MIT" License,
  5.  see LICENSE.md for details.
  6.  -----------------------------------------------------------------------------*/
  7. package mods.railcraft.api.carts;
  8.  
  9. import com.mojang.authlib.GameProfile;
  10. import mods.railcraft.api.core.RailcraftConstantsAPI;
  11. import mods.railcraft.api.core.RailcraftFakePlayer;
  12. import mods.railcraft.api.core.items.IMinecartItem;
  13. import net.minecraft.block.BlockRailBase;
  14. import net.minecraft.entity.item.EntityMinecart;
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.item.ItemMinecart;
  17. import net.minecraft.item.ItemStack;
  18. import net.minecraft.nbt.NBTTagCompound;
  19. import net.minecraft.util.EntitySelectors;
  20. import net.minecraft.util.EnumActionResult;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.util.EnumHand;
  23. import net.minecraft.util.math.AxisAlignedBB;
  24. import net.minecraft.util.math.BlockPos;
  25. import net.minecraft.world.World;
  26. import net.minecraft.world.WorldServer;
  27.  
  28. import javax.annotation.Nullable;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.UUID;
  32.  
  33. @SuppressWarnings({"WeakerAccess", "unused"})
  34. public abstract class CartToolsAPI {
  35.     public static ILinkageManager linkageManager;
  36.     public static ITrainTransferHelper transferHelper;
  37.  
  38.     /**
  39.      * Returns an instance of ILinkageManager.
  40.      * <p/>
  41.      * Will return null if Railcraft is not installed.
  42.      *
  43.      * @param world The World, may be required in the future
  44.      * @return an instance of ILinkageManager
  45.      */
  46.     @SuppressWarnings("UnusedParameters")
  47.     public static ILinkageManager getLinkageManager(World world) {
  48.         return linkageManager;
  49.     }
  50.  
  51.     /**
  52.      * Sets a carts owner.
  53.      * <p/>
  54.      * The is really only needed by the bukkit ports.
  55.      */
  56.     public static void setCartOwner(EntityMinecart cart, EntityPlayer owner) {
  57.         setCartOwner(cart, owner.getGameProfile());
  58.     }
  59.  
  60.     /**
  61.      * Sets a carts owner.
  62.      * <p/>
  63.      * The is really only needed by the bukkit ports.
  64.      */
  65.     public static void setCartOwner(EntityMinecart cart, GameProfile owner) {
  66.         if (!cart.getEntityWorld().isRemote) {
  67.             NBTTagCompound data = cart.getEntityData();
  68.             if (owner.getName() != null)
  69.                 data.setString("owner", owner.getName());
  70.             if (owner.getId() != null)
  71.                 data.setString("ownerId", owner.getId().toString());
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Gets a carts owner. (player.username)
  77.      * <p/>
  78.      * The is really only needed by the bukkit ports.
  79.      */
  80.     public static GameProfile getCartOwner(EntityMinecart cart) {
  81.         NBTTagCompound data = cart.getEntityData();
  82.         String ownerName = RailcraftConstantsAPI.UNKNOWN_PLAYER;
  83.         if (data.hasKey("owner"))
  84.             ownerName = data.getString("owner");
  85.  
  86.         UUID ownerId = null;
  87.         if (data.hasKey("ownerId"))
  88.             ownerId = UUID.fromString(data.getString("ownerId"));
  89.         return new GameProfile(ownerId, ownerName);
  90.     }
  91.  
  92.     /**
  93.      * Does the cart have a owner?
  94.      * <p/>
  95.      * The is really only needed by the bukkit ports.
  96.      */
  97.     public static boolean doesCartHaveOwner(EntityMinecart cart) {
  98.         NBTTagCompound data = cart.getEntityData();
  99.         return data.hasKey("owner");
  100.     }
  101.  
  102.     /**
  103.      * Spawns a new cart entity using the provided item.
  104.      * <p/>
  105.      * The backing item must implement <code>IMinecartItem</code> and/or extend
  106.      * <code>ItemMinecart</code>.
  107.      * <p/>
  108.      * Generally Forge requires all cart items to extend ItemMinecart.
  109.      *
  110.      * @param owner The player name that should used as the owner
  111.      * @param cart  An ItemStack containing a cart item, will not be changed by
  112.      *              the function
  113.      * @param world The World object
  114.      * @return the cart placed or null if failed
  115.      * @see IMinecartItem, ItemMinecart
  116.      */
  117.     @Nullable
  118.     public static EntityMinecart placeCart(GameProfile owner, ItemStack cart, WorldServer world, BlockPos pos) {
  119.         cart = cart.copy();
  120.         if (cart.getItem() instanceof IMinecartItem) {
  121.             IMinecartItem mi = (IMinecartItem) cart.getItem();
  122.             return mi.placeCart(owner, cart, world, pos);
  123.         } else if (cart.getItem() instanceof ItemMinecart)
  124.             try {
  125.                 EnumActionResult placed = cart.getItem().onItemUse(RailcraftFakePlayer.get(world, pos, cart, EnumHand.MAIN_HAND), world, pos, EnumHand.MAIN_HAND, EnumFacing.DOWN, 0, 0, 0);
  126.                 if (placed == EnumActionResult.SUCCESS) {
  127.                     List<EntityMinecart> carts = getMinecartsAt(world, pos, 0.3f);
  128.                     if (carts.size() > 0) {
  129.                         setCartOwner(carts.get(0), owner);
  130.                         return carts.get(0);
  131.                     }
  132.                 }
  133.             } catch (Exception e) {
  134.                 return null;
  135.             }
  136.  
  137.         return null;
  138.     }
  139.  
  140.     public static boolean isMinecartOnRailAt(World world, BlockPos pos, float sensitivity) {
  141.         return isMinecartOnRailAt(world, pos, sensitivity, null, true);
  142.     }
  143.  
  144.     public static boolean isMinecartOnRailAt(World world, BlockPos pos, float sensitivity, @Nullable Class<? extends EntityMinecart> type, boolean subclass) {
  145.         return BlockRailBase.isRailBlock(world, pos) && isMinecartAt(world, pos, sensitivity, type, subclass);
  146.     }
  147.  
  148.     public static boolean isMinecartOnAnySide(World world, BlockPos pos, float sensitivity) {
  149.         return isMinecartOnAnySide(world, pos, sensitivity, null, true);
  150.     }
  151.  
  152.     public static boolean isMinecartOnAnySide(World world, BlockPos pos, float sensitivity, @Nullable Class<? extends EntityMinecart> type, boolean subclass) {
  153.         List<EntityMinecart> list = new ArrayList<EntityMinecart>();
  154.         for (EnumFacing side : EnumFacing.VALUES) {
  155.             list.addAll(getMinecartsOnSide(world, pos, sensitivity, side));
  156.         }
  157.  
  158.         if (type == null)
  159.             return !list.isEmpty();
  160.         else
  161.             for (EntityMinecart cart : list) {
  162.                 if ((subclass && type.isInstance(cart)) || cart.getClass() == type)
  163.                     return true;
  164.             }
  165.         return false;
  166.     }
  167.  
  168.     public static boolean isMinecartAt(World world, BlockPos pos, float sensitivity) {
  169.         return isMinecartAt(world, pos, sensitivity, null, true);
  170.     }
  171.  
  172.     public static boolean isMinecartAt(World world, BlockPos pos, float sensitivity, @Nullable Class<? extends EntityMinecart> type, boolean subclass) {
  173.         List<EntityMinecart> list = getMinecartsAt(world, pos, sensitivity);
  174.  
  175.         if (type == null)
  176.             return !list.isEmpty();
  177.         else
  178.             for (EntityMinecart cart : list) {
  179.                 if ((subclass && type.isInstance(cart)) || cart.getClass() == type)
  180.                     return true;
  181.             }
  182.         return false;
  183.     }
  184.  
  185.     public static List<EntityMinecart> getMinecartsOnAllSides(World world, BlockPos pos, float sensitivity) {
  186.         List<EntityMinecart> carts = new ArrayList<EntityMinecart>();
  187.         for (EnumFacing side : EnumFacing.VALUES) {
  188.             carts.addAll(getMinecartsOnSide(world, pos, sensitivity, side));
  189.         }
  190.  
  191.         return carts;
  192.     }
  193.  
  194.     public static List<EntityMinecart> getMinecartsOnAllSides(World world, BlockPos pos, float sensitivity, Class<? extends EntityMinecart> type, boolean subclass) {
  195.         List<EntityMinecart> list = new ArrayList<EntityMinecart>();
  196.         List<EntityMinecart> carts = new ArrayList<EntityMinecart>();
  197.         for (EnumFacing side : EnumFacing.VALUES) {
  198.             list.addAll(getMinecartsOnSide(world, pos, sensitivity, side));
  199.         }
  200.  
  201.         for (EntityMinecart cart : list) {
  202.             if ((subclass && type.isInstance(cart)) || cart.getClass() == type)
  203.                 carts.add(cart);
  204.         }
  205.         return carts;
  206.     }
  207.  
  208.     public static List<EntityMinecart> getMinecartsOnSide(World world, BlockPos pos, float sensitivity, EnumFacing side) {
  209.         return getMinecartsAt(world, pos.offset(side), sensitivity);
  210.     }
  211.  
  212.     public static boolean isMinecartOnSide(World world, BlockPos pos, float sensitivity, EnumFacing side) {
  213.         return getMinecartOnSide(world, pos, sensitivity, side) != null;
  214.     }
  215.  
  216.     @Nullable
  217.     public static EntityMinecart getMinecartOnSide(World world, BlockPos pos, float sensitivity, EnumFacing side) {
  218.         List<EntityMinecart> carts = getMinecartsOnSide(world, pos, sensitivity, side);
  219.         if (!carts.isEmpty())
  220.             return carts.get(0);
  221.         return null;
  222.     }
  223.  
  224.     public static boolean isMinecartOnSide(World world, BlockPos pos, float sensitivity, EnumFacing side, @Nullable Class<? extends EntityMinecart> type, boolean subclass) {
  225.         return getMinecartOnSide(world, pos, sensitivity, side, type, subclass) != null;
  226.     }
  227.  
  228.     @SuppressWarnings("unchecked")
  229.     @Nullable
  230.     public static <T extends EntityMinecart> T getMinecartOnSide(World world, BlockPos pos, float sensitivity, EnumFacing side, @Nullable Class<T> type, boolean subclass) {
  231.         for (EntityMinecart cart : getMinecartsOnSide(world, pos, sensitivity, side)) {
  232.             if (type == null || (subclass && type.isInstance(cart)) || cart.getClass() == type)
  233.                 return (T) cart;
  234.         }
  235.         return null;
  236.     }
  237.  
  238.     /**
  239.      * @param sensitivity Controls the size of the search box, ranges from
  240.      *                    (-inf, 0.49].
  241.      */
  242.     public static List<EntityMinecart> getMinecartsAt(World world, BlockPos pos, float sensitivity) {
  243.         sensitivity = Math.min(sensitivity, 0.49f);
  244.         return world.getEntitiesWithinAABB(EntityMinecart.class, new AxisAlignedBB(pos.getX() + sensitivity, pos.getY(), pos.getZ() + sensitivity,
  245.                 pos.getX() + 1 - sensitivity, pos.getY() + 1 - sensitivity, pos.getZ() + 1 - sensitivity), EntitySelectors.IS_ALIVE);
  246.     }
  247.  
  248.     public static List<EntityMinecart> getMinecartsIn(World world, BlockPos p1, BlockPos p2) {
  249.         List entities = world.getEntitiesWithinAABB(EntityMinecart.class, new AxisAlignedBB(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ()));
  250.         List<EntityMinecart> carts = new ArrayList<EntityMinecart>();
  251.         for (Object o : entities) {
  252.             EntityMinecart cart = (EntityMinecart) o;
  253.             if (!cart.isDead)
  254.                 carts.add((EntityMinecart) o);
  255.         }
  256.         return carts;
  257.     }
  258.  
  259.     /**
  260.      * Returns the cart's "speed". It is not capped by the carts max speed, it
  261.      * instead returns the cart's "potential" speed. Used by collision and
  262.      * linkage logic. Do not use this to determine how fast a cart is currently
  263.      * moving.
  264.      *
  265.      * @return speed
  266.      */
  267.     public static double getCartSpeedUncapped(EntityMinecart cart) {
  268.         return Math.sqrt(cart.motionX * cart.motionX + cart.motionZ * cart.motionZ);
  269.     }
  270.  
  271.     public static boolean cartVelocityIsLessThan(EntityMinecart cart, float vel) {
  272.         return Math.abs(cart.motionX) < vel && Math.abs(cart.motionZ) < vel;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement