Newer
Older
ultramine_hawkeye / src / main / java / org / ultramine / mods / hawkeye / SearchParser.java
@zaxar163 zaxar163 on 5 Jul 2018 9 KB Inital.
package org.ultramine.mods.hawkeye;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import org.ultramine.mods.hawkeye.util.HawkPermission;
import org.ultramine.mods.hawkeye.util.HawkUtil;
import org.ultramine.mods.hawkeye.util.HawkVector;

public class SearchParser {
   public ICommandSender player = null;
   public List players = new ArrayList();
   public HawkVector loc = null;
   public HawkVector minLoc = null;
   public HawkVector maxLoc = null;
   public Integer radius = null;
   public List actions = new ArrayList();
   public String[] worlds = null;
   public long dateFrom = 0L;
   public long dateTo = 0L;
   public String[] filters = null;

   public SearchParser() {
   }

   public SearchParser(ICommandSender player) {
      this.player = player;
   }

   public SearchParser(ICommandSender player, int radius) {
      this.player = player;
      this.radius = radius;
      this.parseLocations();
   }

   public SearchParser(ICommandSender player, List args) throws IllegalArgumentException {
      this.player = player;
      String lastParam = "";
      boolean paramSet = false;
      boolean worldedit = false;

      for(int i = 0; i < args.size(); ++i) {
         String arg = (String)args.get(i);
         if (!arg.isEmpty()) {
            if (!paramSet) {
               if (arg.length() < 2) {
                  throw new IllegalArgumentException("Invalid argument format: &7" + arg);
               }

               if (!arg.substring(1, 2).equals(":")) {
                  if (arg.contains(":")) {
                     throw new IllegalArgumentException("Invalid argument format: &7" + arg);
                  }

                  this.players.add(arg);
                  continue;
               }

               lastParam = arg.substring(0, 1).toLowerCase();
               paramSet = true;
               if (arg.length() == 2) {
                  if (i == args.size() - 1) {
                     throw new IllegalArgumentException("Invalid argument format: &7" + arg);
                  }
                  continue;
               }

               arg = arg.substring(2);
            }

            if (paramSet) {
               if (arg.isEmpty()) {
                  throw new IllegalArgumentException("Invalid argument format: &7" + lastParam + ":");
               }

               String[] values = arg.split(",");
               String[] var9;
               int weeks;
               int days;
               String value;
               if (lastParam.equals("p")) {
                  var9 = values;
                  weeks = values.length;

                  for(days = 0; days < weeks; ++days) {
                     value = var9[days];
                     this.players.add(value);
                  }
               } else if (lastParam.equals("w")) {
                  this.worlds = values;
               } else if (lastParam.equals("f")) {
                  if (this.filters != null) {
                     this.filters = (String[])HawkUtil.concat(this.filters, values);
                  } else {
                     this.filters = values;
                  }
               } else if (!lastParam.equals("b")) {
                  if (lastParam.equals("a")) {
                     var9 = values;
                     weeks = values.length;

                     for(days = 0; days < weeks; ++days) {
                        value = var9[days];
                        DataType type = DataType.fromName(value);
                        if (type == null) {
                           throw new IllegalArgumentException("Invalid action supplied: &7" + value);
                        }

                        if (!HawkPermission.searchType(player, type.getConfigName())) {
                           throw new IllegalArgumentException("You do not have permission to search for: &7" + type.getConfigName());
                        }

                        this.actions.add(type);
                     }
                  } else if (lastParam.equals("l") && player instanceof EntityPlayerMP) {
                     if (values[0].equalsIgnoreCase("here")) {
                        this.loc = new HawkVector((EntityPlayerMP)player);
                     } else {
                        this.loc = new HawkVector();
                        this.loc.setX(Integer.parseInt(values[0]));
                        this.loc.setY(Integer.parseInt(values[1]));
                        this.loc.setZ(Integer.parseInt(values[2]));
                     }
                  } else if (lastParam.equals("r") && player instanceof EntityPlayerMP) {
                     if (HawkUtil.isInteger(values[0])) {
                        this.radius = Integer.parseInt(values[0]);
                        int maxRadius = HawkEye.instance.config.general.maxRadius;
                        if (maxRadius != 0 && this.radius > maxRadius) {
                           throw new IllegalArgumentException("Radius too large, max allowed: &7" + maxRadius);
                        }
                     }
                  } else {
                     if (!lastParam.equals("t")) {
                        throw new IllegalArgumentException("Invalid parameter supplied: &7" + lastParam);
                     }

                     int type = 2;

                     for(weeks = 0; weeks < arg.length(); ++weeks) {
                        String c = arg.substring(weeks, weeks + 1);
                        if (!HawkUtil.isInteger(c)) {
                           if (c.equals("m") || c.equals("s") || c.equals("h") || c.equals("d") || c.equals("w")) {
                              type = 0;
                           }

                           if (c.equals("-") || c.equals(":")) {
                              type = 1;
                           }
                        }
                     }

                     if (type == 0) {
                        weeks = 0;
                        days = 0;
                        int hours = 0;
                        int mins = 0;
                        int secs = 0;
                        String nums = "";

                        for(int j = 0; j < values[0].length(); ++j) {
                           String c = values[0].substring(j, j + 1);
                           if (HawkUtil.isInteger(c)) {
                              nums = nums + c;
                           } else {
                              int num = Integer.parseInt(nums);
                              if (c.equals("w")) {
                                 weeks = num;
                              } else if (c.equals("d")) {
                                 days = num;
                              } else if (c.equals("h")) {
                                 hours = num;
                              } else if (c.equals("m")) {
                                 mins = num;
                              } else {
                                 if (!c.equals("s")) {
                                    throw new IllegalArgumentException("Invalid time measurement: &7" + c);
                                 }

                                 secs = num;
                              }

                              nums = "";
                           }
                        }

                        Calendar cal = Calendar.getInstance();
                        cal.add(3, -1 * weeks);
                        cal.add(5, -1 * days);
                        cal.add(10, -1 * hours);
                        cal.add(12, -1 * mins);
                        cal.add(13, -1 * secs);
                        this.dateFrom = cal.getTimeInMillis();
                     } else if (type == 1) {
                        try {
                           SimpleDateFormat parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                           if (values.length == 1) {
                              SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");
                              this.dateFrom = parse.parse(form.format(Calendar.getInstance().getTime()) + " " + values[0]).getTime();
                           }

                           if (values.length >= 2) {
                              this.dateFrom = parse.parse(values[0] + " " + values[1]).getTime();
                           }

                           if (values.length == 4) {
                              this.dateTo = parse.parse(values[2] + " " + values[3]).getTime();
                           }
                        } catch (ParseException var19) {
                           throw new IllegalArgumentException("Invalid time format!", var19);
                        }
                     } else if (type == 2) {
                        throw new IllegalArgumentException("Invalid time format!");
                     }
                  }
               }

               paramSet = false;
            }
         }
      }

      if (!worldedit) {
         this.parseLocations();
      }

   }

   public void parseLocations() {
      if (this.player instanceof EntityPlayerMP) {
         int maxRadius = HawkEye.instance.config.general.maxRadius;
         if (this.radius == null && maxRadius != 0) {
            this.radius = maxRadius;
         }

         if (this.radius != null) {
            if (this.loc == null) {
               this.loc = new HawkVector((EntityPlayerMP)this.player);
            }

            if (this.worlds == null) {
               this.worlds = new String[]{Integer.toString(((EntityPlayerMP)this.player).ap)};
            }

            this.minLoc = new HawkVector(this.loc.getX() - (double)this.radius, this.loc.getY() - (double)this.radius, this.loc.getZ() - (double)this.radius);
            this.maxLoc = new HawkVector(this.loc.getX() + (double)this.radius, this.loc.getY() + (double)this.radius, this.loc.getZ() + (double)this.radius);
         }

      }
   }
}