Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wurmonline.server.questions;
- import com.Joedobo27.setserverwearther.BmlForm;
- import com.wurmonline.server.Server;
- import com.wurmonline.server.creatures.Creature;
- import com.wurmonline.server.items.Item;
- import com.wurmonline.server.weather.Weather;
- import org.gotti.wurmunlimited.modloader.ReflectionUtil;
- import java.lang.reflect.InvocationTargetException;
- import java.util.LinkedList;
- import java.util.Properties;
- import java.util.logging.Level;
- public class ServerWeatherQuestion extends Question {
- public ServerWeatherQuestion(Creature responder) {
- super(responder, "Set Weather", "Set these weather values. " +
- "", 500, findWand(responder));
- }
- static long findWand(final Creature performer) {
- final int wand = (performer.getPower() >= 4) ? 176 : 315;
- final Item[] allItems;
- final Item[] inv = allItems = performer.getInventory().getAllItems(false);
- for (final Item item : allItems) {
- if (item.getTemplateId() == wand) {
- return item.getWurmId();
- }
- }
- return -10L;
- }
- @Override
- public void answer(final Properties answer) {
- this.setAnswer(answer);
- if (this.type == 0) {
- logger.log(Level.INFO, "Received answer for a question with NOQUESTION.");
- return;
- }
- if (this.type == 500 && this.getResponder().getPower() >= 4) {
- Weather weather = Server.getWeather();
- final boolean cloudBox = Boolean.parseBoolean(answer.getProperty("cloudBox"));
- if (cloudBox) {
- logger.log(Level.INFO, "cloudiness set to " + cloudBox);
- final float cloudValue = Float.parseFloat(answer.getProperty("cloudValue"));
- weather.setCloudTarget(cloudValue);
- setCloudReflection(cloudValue);
- }
- final boolean fogBox = Boolean.parseBoolean(answer.getProperty("fogBox"));
- if (fogBox) {
- final float fogValue = Float.parseFloat(answer.getProperty("fogValue"));
- logger.log(Level.INFO, "fog set to " + fogValue);
- setFogReflection(fogValue);
- setFogTargetReflection(fogValue);
- }
- final boolean rainBox = Boolean.parseBoolean(answer.getProperty("rainBox"));
- if (rainBox) {
- final float rainValue = Float.parseFloat(answer.getProperty("rainValue"));
- weather.setRainTarget(rainValue);
- }
- final boolean windRotBox = Boolean.parseBoolean(answer.getProperty("windRotBox"));
- final boolean windPowBox = Boolean.parseBoolean(answer.getProperty("windPowBox"));
- if (windRotBox || windPowBox) {
- final float windRotValue = Float.parseFloat(answer.getProperty("windRotValue"));
- final float windPowValue = Float.parseFloat(answer.getProperty("windPowValue"));
- weather.setWindOnly(windRotValue, windPowValue, 1.0f);
- }
- invokeStartSendWeatherThread();
- }
- }
- @Override
- public void sendQuestion() {
- Weather weather = Server.getWeather();
- float cloudiness = weather.getCloudiness();
- float fog = weather.getFog();
- float rain = weather.getRain();
- float windRotation = weather.getWindRotation();
- float windPower = weather.getWindPower();
- // float cloudiness, float fog, float rain, windRotation, windPower
- BmlForm bmlForm = new BmlForm(getQuestion(), 500, 400);
- bmlForm.addHidden("id", ""+this.id);
- bmlForm.beginHorizontalFlow();
- bmlForm.addLabel("Select");
- bmlForm.addLabel("Change");
- bmlForm.addText("-------------------------------------------------------------- ");
- bmlForm.endHorizontalFlow();
- bmlForm.beginHorizontalFlow();
- bmlForm.addCheckBox("cloudBox", false);
- bmlForm.addInput("cloudValue", Float.toString(cloudiness), 10);
- bmlForm.addText("Cloudiness of %1$s.", Float.toString(cloudiness));
- bmlForm.endHorizontalFlow();
- bmlForm.addText("\n");
- bmlForm.beginHorizontalFlow();
- bmlForm.addCheckBox("fogBox", false);
- bmlForm.addInput("fogValue", Float.toString(fog), 10);
- bmlForm.addText("Fogginess of %1$s.", Float.toString(fog));
- bmlForm.endHorizontalFlow();
- bmlForm.addText("\n");
- bmlForm.beginHorizontalFlow();
- bmlForm.addCheckBox("rainBox", false);
- bmlForm.addInput("rainValue", Float.toString(rain), 10);
- bmlForm.addText("raininess of %1$s.", Float.toString(rain));
- bmlForm.endHorizontalFlow();
- bmlForm.addText("\n");
- bmlForm.beginHorizontalFlow();
- bmlForm.addCheckBox("windRotBox", false);
- bmlForm.addInput("windRotValue", Float.toString(windRotation), 10);
- bmlForm.addText("Wind orientation at %1$s.", Float.toString(windRotation));
- bmlForm.endHorizontalFlow();
- bmlForm.addText("\n");
- bmlForm.beginHorizontalFlow();
- bmlForm.addCheckBox("windPowBox", false);
- bmlForm.addInput("windPowValue", Float.toString(windPower), 10);
- bmlForm.addText("Wind power at %1$s.", Float.toString(windPower));
- bmlForm.endHorizontalFlow();
- bmlForm.addText("\nFog spider count: %1$s", Integer.toString(getFogSpiderCount()));
- bmlForm.addButton("Send", "submit");
- String bml = bmlForm.toString();
- //logger.log(Level.INFO, bml);
- this.getResponder().getCommunicator().sendBml(500, 500, true, true,
- bml, 200, 200, 200, this.title);
- }
- private void setCloudReflection(float cloudValue) {
- Weather weather = Server.getWeather();
- try {
- ReflectionUtil.setPrivateField(weather,
- ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "cloudiness"),
- cloudValue);
- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
- logger.log(Level.WARNING, e.getMessage(), e);
- }
- }
- private void setFogReflection(float fogValue) {
- Weather weather = Server.getWeather();
- try {
- ReflectionUtil.setPrivateField(weather,
- ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "fog"),
- fogValue);
- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
- logger.log(Level.WARNING, e.getMessage(), e);
- }
- }
- private void setFogTargetReflection(float fogValue) {
- Weather weather = Server.getWeather();
- try {
- ReflectionUtil.setPrivateField(weather,
- ReflectionUtil.getField(Class.forName("com.wurmonline.server.weather.Weather"), "fogTarget"),
- fogValue);
- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
- logger.log(Level.WARNING, e.getMessage(), e);
- }
- }
- private int getFogSpiderCount() {
- LinkedList<Long> fogSpiders = new LinkedList<>();
- try {
- fogSpiders = ReflectionUtil.getPrivateField(Class.forName("com.wurmonline.server.zones.Zone"),
- ReflectionUtil.getField(Class.forName("com.wurmonline.server.zones.Zone"), "fogSpiders"));
- }catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e){
- logger.log(Level.WARNING, e.getMessage(), e);
- }
- return fogSpiders.size();
- }
- private void invokeStartSendWeatherThread() {
- try {
- ReflectionUtil.callPrivateMethod(Class.forName("com.wurmonline.server.Server"), ReflectionUtil.getMethod(
- Class.forName("com.wurmonline.server.Server"), "startSendWeatherThread"), null);
- }catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
- logger.log(Level.WARNING, e.getMessage(), e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement