Advertisement
GravityCube

Untitled

Feb 7th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package ee.lutsu.alpha.mc.mytown.event.prot;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7. import java.util.Map.Entry;
  8.  
  9. import ee.lutsu.alpha.mc.mytown.ChatChannel;
  10. import ee.lutsu.alpha.mc.mytown.Formatter;
  11. import ee.lutsu.alpha.mc.mytown.MyTown;
  12. import ee.lutsu.alpha.mc.mytown.MyTownDatasource;
  13. import ee.lutsu.alpha.mc.mytown.cmd.CmdChat;
  14. import ee.lutsu.alpha.mc.mytown.entities.TownBlock;
  15. import ee.lutsu.alpha.mc.mytown.event.ProtBase;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.util.ChunkCoordinates;
  18. import universalelectricity.api.vector.Vector3;
  19.  
  20. public class MFFS extends ProtBase {
  21.     public static MFFS instance = new MFFS();
  22.  
  23.     Class<?> forceField = null;
  24.     Method mSetActivate, mIsOn, mGetField;
  25.  
  26.     public HashMap<Object, Boolean> lastAllowed = new HashMap<Object, Boolean>();
  27.  
  28.     public HashMap<ChunkCoordinates, Long> anti_spam = new HashMap<ChunkCoordinates, Long>();
  29.     public HashMap<Object, Long> anti_spam_calc = new HashMap<Object, Long>();
  30.     public int anti_spam_counter = 0;
  31.  
  32.     @Override
  33.     public void reload() {
  34.         anti_spam_counter = 0;
  35.         lastAllowed = new HashMap<Object, Boolean>();
  36.         anti_spam = new HashMap<ChunkCoordinates, Long>();
  37.         anti_spam_calc = new HashMap<Object, Long>();
  38.     }
  39.  
  40.     @Override
  41.     public void load() throws Exception {
  42.         forceField = Class.forName("mffs.tile.TileForceFieldProjector");
  43.         mGetField = forceField.getMethod("getCalculatedField");
  44.         mSetActivate = forceField.getMethod("setActive", boolean.class);
  45.         mIsOn = forceField.getMethod("isActive");
  46.     }
  47.  
  48.     @Override
  49.     public boolean loaded() {
  50.         return forceField != null;
  51.     }
  52.  
  53.     @Override
  54.     public boolean isEntityInstance(TileEntity e) {
  55.         return forceField.isInstance(e);
  56.     }
  57.  
  58.     @Override
  59.     public String update(TileEntity e) throws Exception {
  60.         if (!(Boolean)mIsOn.invoke(e)) {
  61.             return null;
  62.         }
  63.         cleanAntiSpam();
  64.         Set<Vector3> calculatedField = (Set<Vector3>)mGetField.invoke(e);
  65.         if (lastAllowed.containsKey(e)){
  66.             if (!lastAllowed.get(e)) {
  67.                 blockAction(e);
  68.             }
  69.             return null;
  70.         }
  71.         for (Vector3 vector: calculatedField){
  72.             int x = vector.intX();
  73.             int z = vector.intZ();
  74.             int dim = e.worldObj.provider.dimensionId;
  75.             boolean allowed = canRoam(dim, x, 60, 60, z);
  76.            
  77.             if (!allowed) {
  78.                 blockAction(e);
  79.                 lastAllowed.put(e, false);
  80.                 anti_spam_calc.put(e, System.currentTimeMillis() + 1000);
  81.                 return null;
  82.             }
  83.         }
  84.         if (calculatedField.size() > 10) {
  85.             lastAllowed.put(e, true);
  86.             anti_spam_calc.put(e, System.currentTimeMillis() + 1000);
  87.         }
  88.         return null;
  89.     }
  90.  
  91.     private boolean canRoam(int dim, int x, int y, int y2, int z) {
  92.         TownBlock b = MyTownDatasource.instance.getPermBlockAtCoord(dim, x, y, y2, z);
  93.  
  94.         if (b == null || b.town() == null) {
  95.             return MyTown.instance.getWorldWildSettings(dim).allowMFFS;
  96.         }
  97.  
  98.         return b.settings.allowMFFS;
  99.     }
  100.     private void blockAction(TileEntity e) throws Exception  {
  101.         mSetActivate.invoke(e, false);
  102.         ChunkCoordinates c = new ChunkCoordinates(e.xCoord, e.yCoord, e.zCoord);
  103.         if (canScream(c)) {
  104.             MyTown.instance.bypassLog.severe(String.format("ยง4Stopped a projector found @ dim %s, %s,%s,%s", e.worldObj.provider.dimensionId, e.xCoord, e.yCoord, e.zCoord));
  105.  
  106.             String msg = String.format("A projector stopped @ %s,%s,%s because it wasn't allowed there", e.xCoord, e.yCoord, e.zCoord);
  107.             String formatted = Formatter.formatChatSystem(msg, ChatChannel.Local);
  108.             CmdChat.sendChatToAround(e.worldObj.provider.dimensionId, e.xCoord, e.yCoord, e.zCoord, formatted, null);
  109.  
  110.             anti_spam.put(c, System.currentTimeMillis() + 60000);
  111.         }
  112.     }
  113.  
  114.     private boolean canScream(ChunkCoordinates c) {
  115.         return anti_spam.get(c) == null;
  116.     }
  117.  
  118.     private void cleanAntiSpam() {
  119.         anti_spam_counter++;
  120.  
  121.         if (anti_spam_counter > 200) {
  122.             anti_spam_counter = 0;
  123.             long time = System.currentTimeMillis();
  124.  
  125.             for (Iterator<Entry<Object, Long>> it = anti_spam_calc.entrySet().iterator(); it.hasNext();) {
  126.                 Entry<Object, Long> kv = it.next();
  127.                 if (kv.getValue() < time) {
  128.                     lastAllowed.remove(kv.getKey());
  129.                     it.remove();
  130.                 }
  131.             }
  132.            
  133.             for (Iterator<Entry<ChunkCoordinates, Long>> it = anti_spam.entrySet().iterator(); it.hasNext();) {
  134.                 Entry<ChunkCoordinates, Long> kv = it.next();
  135.                 if (kv.getValue() < time) {
  136.                     it.remove();
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.     @Override
  143.     public String getMod() {
  144.         return "MFFS";
  145.     }
  146.  
  147.     @Override
  148.     public String getComment() {
  149.         return "Town permission: mffs ";
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement