Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.lutsu.alpha.mc.mytown.event.prot;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.Map.Entry;
- import ee.lutsu.alpha.mc.mytown.ChatChannel;
- import ee.lutsu.alpha.mc.mytown.Formatter;
- import ee.lutsu.alpha.mc.mytown.MyTown;
- import ee.lutsu.alpha.mc.mytown.MyTownDatasource;
- import ee.lutsu.alpha.mc.mytown.cmd.CmdChat;
- import ee.lutsu.alpha.mc.mytown.entities.TownBlock;
- import ee.lutsu.alpha.mc.mytown.event.ProtBase;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.ChunkCoordinates;
- import universalelectricity.api.vector.Vector3;
- public class MFFS extends ProtBase {
- public static MFFS instance = new MFFS();
- Class<?> forceField = null;
- Method mSetActivate, mIsOn, mGetField;
- public HashMap<Object, Boolean> lastAllowed = new HashMap<Object, Boolean>();
- public HashMap<ChunkCoordinates, Long> anti_spam = new HashMap<ChunkCoordinates, Long>();
- public HashMap<Object, Long> anti_spam_calc = new HashMap<Object, Long>();
- public int anti_spam_counter = 0;
- @Override
- public void reload() {
- anti_spam_counter = 0;
- lastAllowed = new HashMap<Object, Boolean>();
- anti_spam = new HashMap<ChunkCoordinates, Long>();
- anti_spam_calc = new HashMap<Object, Long>();
- }
- @Override
- public void load() throws Exception {
- forceField = Class.forName("mffs.tile.TileForceFieldProjector");
- mGetField = forceField.getMethod("getCalculatedField");
- mSetActivate = forceField.getMethod("setActive", boolean.class);
- mIsOn = forceField.getMethod("isActive");
- }
- @Override
- public boolean loaded() {
- return forceField != null;
- }
- @Override
- public boolean isEntityInstance(TileEntity e) {
- return forceField.isInstance(e);
- }
- @Override
- public String update(TileEntity e) throws Exception {
- if (!(Boolean)mIsOn.invoke(e)) {
- return null;
- }
- cleanAntiSpam();
- Set<Vector3> calculatedField = (Set<Vector3>)mGetField.invoke(e);
- if (lastAllowed.containsKey(e)){
- if (!lastAllowed.get(e)) {
- blockAction(e);
- }
- return null;
- }
- for (Vector3 vector: calculatedField){
- int x = vector.intX();
- int z = vector.intZ();
- int dim = e.worldObj.provider.dimensionId;
- boolean allowed = canRoam(dim, x, 60, 60, z);
- if (!allowed) {
- blockAction(e);
- lastAllowed.put(e, false);
- anti_spam_calc.put(e, System.currentTimeMillis() + 1000);
- return null;
- }
- }
- if (calculatedField.size() > 10) {
- lastAllowed.put(e, true);
- anti_spam_calc.put(e, System.currentTimeMillis() + 1000);
- }
- return null;
- }
- private boolean canRoam(int dim, int x, int y, int y2, int z) {
- TownBlock b = MyTownDatasource.instance.getPermBlockAtCoord(dim, x, y, y2, z);
- if (b == null || b.town() == null) {
- return MyTown.instance.getWorldWildSettings(dim).allowMFFS;
- }
- return b.settings.allowMFFS;
- }
- private void blockAction(TileEntity e) throws Exception {
- mSetActivate.invoke(e, false);
- ChunkCoordinates c = new ChunkCoordinates(e.xCoord, e.yCoord, e.zCoord);
- if (canScream(c)) {
- 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));
- String msg = String.format("A projector stopped @ %s,%s,%s because it wasn't allowed there", e.xCoord, e.yCoord, e.zCoord);
- String formatted = Formatter.formatChatSystem(msg, ChatChannel.Local);
- CmdChat.sendChatToAround(e.worldObj.provider.dimensionId, e.xCoord, e.yCoord, e.zCoord, formatted, null);
- anti_spam.put(c, System.currentTimeMillis() + 60000);
- }
- }
- private boolean canScream(ChunkCoordinates c) {
- return anti_spam.get(c) == null;
- }
- private void cleanAntiSpam() {
- anti_spam_counter++;
- if (anti_spam_counter > 200) {
- anti_spam_counter = 0;
- long time = System.currentTimeMillis();
- for (Iterator<Entry<Object, Long>> it = anti_spam_calc.entrySet().iterator(); it.hasNext();) {
- Entry<Object, Long> kv = it.next();
- if (kv.getValue() < time) {
- lastAllowed.remove(kv.getKey());
- it.remove();
- }
- }
- for (Iterator<Entry<ChunkCoordinates, Long>> it = anti_spam.entrySet().iterator(); it.hasNext();) {
- Entry<ChunkCoordinates, Long> kv = it.next();
- if (kv.getValue() < time) {
- it.remove();
- }
- }
- }
- }
- @Override
- public String getMod() {
- return "MFFS";
- }
- @Override
- public String getComment() {
- return "Town permission: mffs ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement