Guest User

Untitled

a guest
Jan 5th, 2019
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.69 KB | None | 0 0
  1. package drake.aepvp.util;
  2.  
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. import drake.aepvp.datatables.EventTemplateTables;
  8. import drake.aepvp.drivers.PlayerDriver;
  9. import drake.aepvp.instance.world.AbstractWorld;
  10. import drake.aepvp.l2event.AbstractEvent;
  11. import drake.aepvp.model.interfaces.IConditional;
  12. import drake.aepvp.model.interfaces.IInfoPackager.IBooleanPackager;
  13. import drake.aepvp.model.interfaces.IInfoPackager.IFloatPackager;
  14. import drake.aepvp.model.interfaces.IInfoPackager.IIntPackager;
  15. import drake.aepvp.model.template.world.EventTemplate;
  16. import drake.aepvp.model.template.world.InstanceTemplate;
  17. import drake.aepvp.model.template.world.WorldTemplate;
  18. import l2.ae.pvp.gameserver.model.Location;
  19. import l2.ae.pvp.gameserver.model.actor.instance.L2PcInstance;
  20. import l2.ae.pvp.gameserver.util.Util;
  21. import l2.ae.pvp.util.Rnd;
  22.  
  23. public class EventUtils
  24. {
  25.     public static final IBooleanPackager retfalse = (pack) -> {return false;};
  26.     public static final IBooleanPackager rettrue = (pack) -> {return true;};
  27.     public static final IFloatPackager ret1f = (pack) -> {return 1f;};
  28.     public static final IIntPackager ret0 = (pack) -> {return 0;};
  29.     public static final IIntPackager ret1 = (pack) -> {return 1;};
  30.    
  31.     public static String prepareString(String msg, PlayerDriver playerDriver)
  32.     {
  33.         return msg.replaceAll("\\$player", String.valueOf(playerDriver.getPlayerName()))
  34.                   .replaceAll("\\$kills", String.valueOf(playerDriver.getEventKills()))
  35.                   .replaceAll("\\$flags", String.valueOf(playerDriver.getEventFlags()))
  36.                   .replaceAll("\\$deaths", String.valueOf(playerDriver.getEventDeaths()))
  37.                   .replaceAll("\\$top", formatPosition(playerDriver.getEventPosition()));
  38.     }
  39.    
  40.     public static String prepareString(String msg, PlayerDriver playerDriver, InstanceTemplate template)
  41.     {
  42.         msg = msg.replaceAll("\\$name", template.getName());
  43.         return prepareString(msg, playerDriver);
  44.     }
  45.    
  46.     public static <T> boolean contains(final T[] array, final T element)
  47.     {
  48.         if (array == null || element == null)
  49.             return false;
  50.         for (T t : array) if (t == element)
  51.             return true;
  52.         return false;
  53.     }
  54.    
  55.     public static boolean makeEvent(final L2PcInstance caller, final String eventId)
  56.     {
  57.         final EventTemplate template = EventTemplateTables.getInstance().getTemplate(eventId);
  58.         if (template == null)
  59.         {
  60.             if (caller == null)
  61.                 System.out.println("Aborting #" + eventId + " creation because its corrupted or it doesnt exist!");
  62.             else
  63.             {
  64.                 caller.sendMessage("Aborting #" + eventId + " creation because its corrupted or it doesnt exist!");
  65.                 caller.sendMessage("Press //eventinfo to list all available #eventIds");
  66.             }
  67.             return false;
  68.         }
  69.         for (AbstractEvent evt : AbstractEvent.getCurrentEvents())
  70.         {
  71.             final EventTemplate activeTemplate = evt.getTemplate();
  72.             if (activeTemplate.getId().equals(template.getId()))
  73.             {
  74.                 if (caller == null)
  75.                     System.out.println("Aborting #" + eventId + " creation because its already active!");
  76.                 else
  77.                     caller.sendMessage("Aborting #" + eventId + " creation because its already active!");
  78.                 return false;
  79.             }
  80.             if (Util.checkIfInRange(200, new Location(activeTemplate.getRegistrationLoc()), new Location(template.getRegistrationLoc()), false))
  81.             {
  82.                 if (caller == null)
  83.                     System.out.println("Aborting #" + eventId + " creation because its registration is very close to #" + activeTemplate.getName() + " 's registration area.");
  84.                 else
  85.                     caller.sendMessage("Aborting #" + eventId + " creation because its registration is very close to #" + activeTemplate.getName() + " 's registration area.");
  86.                 return false;
  87.             }
  88.         }
  89.         try
  90.         {  
  91.             final AbstractEvent event = mkEvent(template);
  92.             if (event != null)
  93.             {
  94.                 event.startEvent();
  95.                 return true;
  96.             }
  97.             return false;
  98.         }
  99.         catch (Exception e)
  100.         {   e.printStackTrace();
  101.         }
  102.         return false;
  103.     }
  104.    
  105.     public static AbstractEvent mkEvent(final EventTemplate eventTemplate)
  106.     {
  107.         final Class<?> eventClass = eventTemplate.getWorldClass();
  108.         try
  109.         {
  110.             return (AbstractEvent) eventClass.getConstructor(EventTemplate.class).newInstance(eventTemplate);
  111.         }
  112.         catch (Exception e)
  113.         {
  114.             e.printStackTrace();
  115.         }
  116.         return null;
  117.     }
  118.    
  119.     public static <V> V jackpotPick(Collection<V> list, IJackpot<V> ijackpot)
  120.     {
  121.         if (list.isEmpty())
  122.             return null;
  123.         if (list.size() == 1)
  124.             for (final V v : list)
  125.                 return v;
  126.         int total = 0;
  127.         for (final V v : list)
  128.             total += ijackpot.getJackpotRate(v);
  129.         final int roll = Rnd.get(0, total);
  130.         int pool = 0;
  131.         for (final V v : list)
  132.         {
  133.             pool += ijackpot.getJackpotRate(v);
  134.             if (pool >= roll)
  135.                 return v;
  136.         }
  137.         return null;
  138.     }
  139.    
  140.     public static <V> V jackpotPick(V[] list, IJackpot<V> ijackpot)
  141.     {
  142.         if (list.length == 0)
  143.             return null;
  144.         if (list.length == 1)
  145.             for (final V v : list)
  146.                 return v;
  147.         int total = 0;
  148.         for (final V v : list)
  149.             total += ijackpot.getJackpotRate(v);
  150.         final int roll = Rnd.get(0, total);
  151.         int pool = 0;
  152.         for (final V v : list)
  153.         {
  154.             pool += ijackpot.getJackpotRate(v);
  155.             if (pool >= roll)
  156.                 return v;
  157.         }
  158.         return null;
  159.     }
  160.    
  161.     public static <V> V jackpotPick(List<V> list, IJackpot<V> ijackpot, V defaultValue, IConditional<V> cond)
  162.     {   final int listSize = list.size();
  163.         if (listSize > 1)
  164.         {   if (cond == null)
  165.             {   int total = 0;
  166.                 for (final V v : list)
  167.                     total += ijackpot.getJackpotRate(v);
  168.                 final int roll = Rnd.get(0, total);
  169.                 int pool = 0;
  170.                 for (final V v : list)
  171.                 {   pool += ijackpot.getJackpotRate(v);
  172.                     if (pool >= roll)
  173.                         return v;
  174.                 }  
  175.             }
  176.             else
  177.                 return jackpotPick(cond.validate(list), ijackpot, defaultValue, null);
  178.         }
  179.         else if (listSize == 1)
  180.         {   final V value = list.get(0);
  181.             if (cond == null)
  182.                 return value;
  183.             return cond.validate(value);
  184.         }
  185.         else if (cond != null)
  186.             return cond.validate(defaultValue);
  187.         return defaultValue;
  188.     }
  189.    
  190.     public static JackpotItem[] parseJackpotItems(final String str)
  191.     {
  192.         if (str == null)
  193.             return null;
  194.         final String[] jackpotItemsStr = str.replace(" ", "").split(",");
  195.         final JackpotItem[] jackpotItems = new JackpotItem[jackpotItemsStr.length];
  196.         for (int i = 0; i < jackpotItemsStr.length; i++)
  197.             jackpotItems[i] = parseJackpotItem(jackpotItemsStr[i]);
  198.         return jackpotItems;
  199.     }
  200.    
  201.     public static JackpotItem[] parseJackpotItemsOrNull(final String str)
  202.     {
  203.         final JackpotItem[] jackpotItems = parseJackpotItems(str);
  204.        
  205.         return jackpotItems == null || jackpotItems.length == 0 ? null : jackpotItems;
  206.     }
  207.    
  208.     public static JackpotItem parseJackpotItem(final String str)
  209.     {
  210.         final String[] jackpotItemStr = str.replace(" ", "").split(":");
  211.         try
  212.         {
  213.             return new JackpotItem(Integer.parseInt(jackpotItemStr[0]), Integer.parseInt(jackpotItemStr[1]));
  214.         }
  215.         catch (Exception e)
  216.         {
  217.             System.out.println("Failed to parse Jackpot item... Make sure it looks like X:N !");
  218.             e.printStackTrace();
  219.             return null;
  220.         }
  221.     }
  222.    
  223.     public static JackpotItemEx[] parseJackpotItemsEx(final String str)
  224.     {      
  225.         if (str == null)
  226.             return null;
  227.         final String[] jackpotItemsStr = str.replace(" ", "").split(",");
  228.         final JackpotItemEx[] jackpotItems = new JackpotItemEx[jackpotItemsStr.length];
  229.         for (int i = 0; i < jackpotItemsStr.length; i++)
  230.             jackpotItems[i] = parseJackpotItemEx(jackpotItemsStr[i]);
  231.         return jackpotItems;
  232.     }
  233.    
  234.     public static JackpotItemEx[] parseJackpotItemsExOrNull(final String str)
  235.     {
  236.         final JackpotItemEx[] jackpotItems = parseJackpotItemsEx(str);
  237.        
  238.         return jackpotItems == null || jackpotItems.length == 0 ? null : jackpotItems;
  239.     }  
  240.     public static JackpotItemEx parseJackpotItemEx(final String str)
  241.     {
  242.         final String[] jackpotItemStr = str.replace(" ", "").split(":");
  243.        
  244.  
  245.        
  246.         try
  247.         {
  248.             final String[] jackpotItemStrEx = jackpotItemStr[0].split("-");
  249.            
  250.             int value0 = 0;
  251.             int value1 = 0;
  252.            
  253.             if (jackpotItemStrEx.length == 1)
  254.                 value0 = value1 = Integer.parseInt(jackpotItemStr[0]);
  255.             else
  256.             {
  257.                 value0 = Integer.parseInt(jackpotItemStrEx[0]);
  258.                 value1 = Integer.parseInt(jackpotItemStrEx[1]);
  259.             }
  260.            
  261.             return new JackpotItemEx(value0, value1, Integer.parseInt(jackpotItemStr[1]));
  262.         }
  263.         catch (Exception e)
  264.         {
  265.             e.printStackTrace();
  266.             return null;
  267.         }
  268.     }
  269.    
  270.     public static class JackpotItem
  271.     {
  272.         private final int _value0;
  273.         private final int _weight;
  274.        
  275.         public JackpotItem(final int value0, final int weight)
  276.         {
  277.             _value0 = value0;
  278.             _weight = weight;
  279.         }
  280.        
  281.         public int getValue()
  282.         {
  283.             return _value0;
  284.         }
  285.        
  286.         public int getWeight()
  287.         {
  288.             return _weight;
  289.         }
  290.     }
  291.    
  292.     public static class JackpotItemEx extends JackpotItem
  293.     {
  294.         private final int _value1;
  295.        
  296.         public JackpotItemEx(final int value0, final int value1, final int weight)
  297.         {
  298.             super(value0, weight);
  299.             _value1 = value1;
  300.         }
  301.        
  302.         public int getValueEx()
  303.         {
  304.             return _value1;
  305.         }
  306.     }
  307.    
  308.     public interface IJackpot<V>
  309.     {
  310.         public int getJackpotRate(V v);
  311.     }
  312.    
  313.     public static String formatPosition(int place)
  314.     {
  315.         switch (place)
  316.         {   case 1:
  317.                 return "1st";
  318.             case 2:
  319.                 return "2nd";
  320.             case 3:
  321.                 return "3rd";
  322.             default:
  323.                 return place + "th";
  324.         }
  325.     }
  326.    
  327.     public static AbstractWorld<?> newWorld(final WorldTemplate worldTemplate) throws Exception
  328.     {
  329.         final Class<?> worldClass = worldTemplate.getWorldClass();
  330.         if (worldClass == null)
  331.             return new AbstractWorld<WorldTemplate>(worldTemplate){};
  332.         return (AbstractWorld<?>) worldClass.getConstructor(worldTemplate.getClass()).newInstance(worldTemplate);
  333.     }
  334.    
  335.     public static long getTimePassed(final long t0, TimeUnit time)
  336.     {
  337.         final long t1 = System.currentTimeMillis();
  338.         if (t0 > t1)
  339.             return 0;
  340.         return time.convert(t1 - t0, TimeUnit.MILLISECONDS);
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment