Advertisement
Anon5703

Untitled

Jul 17th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. package com.wurmonline.server.questions;
  2.  
  3.  
  4. import com.Joedobo27.setserverwearther.BmlForm;
  5. import com.wurmonline.server.Server;
  6. import com.wurmonline.server.creatures.Creature;
  7. import com.wurmonline.server.items.Item;
  8. import com.wurmonline.server.weather.Weather;
  9. import org.gotti.wurmunlimited.modloader.ReflectionUtil;
  10.  
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.LinkedList;
  13. import java.util.Properties;
  14. import java.util.logging.Level;
  15.  
  16. public class ServerWeatherQuestion extends Question {
  17.  
  18.  
  19.     public ServerWeatherQuestion(Creature responder) {
  20.         super(responder, "Set Weather", "Set these weather values.                          " +
  21.                 "", 500, findWand(responder));
  22.     }
  23.  
  24.     static long findWand(final Creature performer) {
  25.         final int wand = (performer.getPower() >= 4) ? 176 : 315;
  26.         final Item[] allItems;
  27.         final Item[] inv = allItems = performer.getInventory().getAllItems(false);
  28.         for (final Item item : allItems) {
  29.             if (item.getTemplateId() == wand) {
  30.                 return item.getWurmId();
  31.             }
  32.         }
  33.         return -10L;
  34.     }
  35.  
  36.     @Override
  37.     public void answer(final Properties answer) {
  38.         this.setAnswer(answer);
  39.         if (this.type == 0) {
  40.             logger.log(Level.INFO, "Received answer for a question with NOQUESTION.");
  41.             return;
  42.         }
  43.         if (this.type == 500 && this.getResponder().getPower() >= 4) {
  44.             Weather weather = Server.getWeather();
  45.             final boolean cloudBox = Boolean.parseBoolean(answer.getProperty("cloudBox"));
  46.             if (cloudBox) {
  47.                 logger.log(Level.INFO, "cloudiness set to " + cloudBox);
  48.                 final float cloudValue = Float.parseFloat(answer.getProperty("cloudValue"));
  49.                 weather.setCloudTarget(cloudValue);
  50.                 setCloudReflection(cloudValue);
  51.             }
  52.             final boolean fogBox = Boolean.parseBoolean(answer.getProperty("fogBox"));
  53.             if (fogBox) {
  54.                 final float fogValue = Float.parseFloat(answer.getProperty("fogValue"));
  55.                 logger.log(Level.INFO, "fog set to " + fogValue);
  56.                 setFogReflection(fogValue);
  57.                 setFogTargetReflection(fogValue);
  58.             }
  59.             final boolean rainBox = Boolean.parseBoolean(answer.getProperty("rainBox"));
  60.             if (rainBox) {
  61.                 final float rainValue = Float.parseFloat(answer.getProperty("rainValue"));
  62.                 weather.setRainTarget(rainValue);
  63.             }
  64.             final boolean windRotBox = Boolean.parseBoolean(answer.getProperty("windRotBox"));
  65.             final boolean windPowBox = Boolean.parseBoolean(answer.getProperty("windPowBox"));
  66.             if (windRotBox || windPowBox) {
  67.                 final float windRotValue = Float.parseFloat(answer.getProperty("windRotValue"));
  68.                 final float windPowValue = Float.parseFloat(answer.getProperty("windPowValue"));
  69.                 weather.setWindOnly(windRotValue, windPowValue, 1.0f);
  70.             }
  71.             invokeStartSendWeatherThread();
  72.         }
  73.     }
  74.  
  75.     @Override
  76.     public void sendQuestion() {
  77.  
  78.         Weather weather = Server.getWeather();
  79.         float cloudiness = weather.getCloudiness();
  80.         float fog = weather.getFog();
  81.         float rain = weather.getRain();
  82.         float windRotation = weather.getWindRotation();
  83.         float windPower = weather.getWindPower();
  84.         // float cloudiness, float fog, float rain, windRotation, windPower
  85.  
  86.         BmlForm bmlForm = new BmlForm(getQuestion(), 500, 400);
  87.         bmlForm.addHidden("id", ""+this.id);
  88.         bmlForm.beginHorizontalFlow();
  89.         bmlForm.addLabel("Select");
  90.         bmlForm.addLabel("Change");
  91.         bmlForm.addText("-------------------------------------------------------------- ");
  92.         bmlForm.endHorizontalFlow();
  93.         bmlForm.beginHorizontalFlow();
  94.         bmlForm.addCheckBox("cloudBox", false);
  95.         bmlForm.addInput("cloudValue", Float.toString(cloudiness), 10);
  96.         bmlForm.addText("Cloudiness of %1$s.", Float.toString(cloudiness));
  97.         bmlForm.endHorizontalFlow();
  98.         bmlForm.addText("\n");
  99.         bmlForm.beginHorizontalFlow();
  100.         bmlForm.addCheckBox("fogBox", false);
  101.         bmlForm.addInput("fogValue", Float.toString(fog), 10);
  102.         bmlForm.addText("Fogginess of %1$s.", Float.toString(fog));
  103.         bmlForm.endHorizontalFlow();
  104.         bmlForm.addText("\n");
  105.         bmlForm.beginHorizontalFlow();
  106.         bmlForm.addCheckBox("rainBox", false);
  107.         bmlForm.addInput("rainValue", Float.toString(rain), 10);
  108.         bmlForm.addText("raininess of %1$s.", Float.toString(rain));
  109.         bmlForm.endHorizontalFlow();
  110.         bmlForm.addText("\n");
  111.         bmlForm.beginHorizontalFlow();
  112.         bmlForm.addCheckBox("windRotBox", false);
  113.         bmlForm.addInput("windRotValue", Float.toString(windRotation), 10);
  114.         bmlForm.addText("Wind orientation at %1$s.", Float.toString(windRotation));
  115.         bmlForm.endHorizontalFlow();
  116.         bmlForm.addText("\n");
  117.         bmlForm.beginHorizontalFlow();
  118.         bmlForm.addCheckBox("windPowBox", false);
  119.         bmlForm.addInput("windPowValue", Float.toString(windPower), 10);
  120.         bmlForm.addText("Wind power at %1$s.", Float.toString(windPower));
  121.         bmlForm.endHorizontalFlow();
  122.         bmlForm.addText("\nFog spider count: %1$s", Integer.toString(getFogSpiderCount()));
  123.         bmlForm.addButton("Send", "submit");
  124.         String bml = bmlForm.toString();
  125.         //logger.log(Level.INFO, bml);
  126.         this.getResponder().getCommunicator().sendBml(500, 500, true, true,
  127.                 bml, 200, 200, 200, this.title);
  128.     }
  129.  
  130.     private void setCloudReflection(float cloudValue) {
  131.         Weather weather = Server.getWeather();
  132.         try {
  133.             ReflectionUtil.setPrivateField(weather,
  134.                     ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "cloudiness"),
  135.                     cloudValue);
  136.         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
  137.             logger.log(Level.WARNING, e.getMessage(), e);
  138.         }
  139.     }
  140.  
  141.     private void setFogReflection(float fogValue) {
  142.         Weather weather = Server.getWeather();
  143.         try {
  144.             ReflectionUtil.setPrivateField(weather,
  145.                     ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "fog"),
  146.                     fogValue);
  147.         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
  148.             logger.log(Level.WARNING, e.getMessage(), e);
  149.         }
  150.     }
  151.  
  152.     private void setFogTargetReflection(float fogValue) {
  153.         Weather weather = Server.getWeather();
  154.         try {
  155.             ReflectionUtil.setPrivateField(weather,
  156.                     ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "fogTarget"),
  157.                     fogValue);
  158.         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
  159.             logger.log(Level.WARNING, e.getMessage(), e);
  160.         }
  161.     }
  162.  
  163.     private int getFogSpiderCount() {
  164.         LinkedList<Long> fogSpiders = new LinkedList<>();
  165.         try {
  166.             fogSpiders = ReflectionUtil.getPrivateField(Class.forName("com.wurmonline.server.zones.Zone"),
  167.                     ReflectionUtil.getField(Class.forName("com.wurmonline.server.zones.Zone"), "fogSpiders"));
  168.         }catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e){
  169.             logger.log(Level.WARNING, e.getMessage(), e);
  170.         }
  171.         return fogSpiders.size();
  172.     }
  173.  
  174.     private void invokeStartSendWeatherThread() {
  175.         try {
  176.             ReflectionUtil.callPrivateMethod(Class.forName("com.wurmonline.server.Server"), ReflectionUtil.getMethod(
  177.                     Class.forName("com.wurmonline.server.Server"), "startSendWeatherThread"), null);
  178.         }catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
  179.             logger.log(Level.WARNING, e.getMessage(), e);
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement