From 2389d544906978104b76ca4af9db1ffa64c23548 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Tue, 5 Jul 2016 20:05:36 +0200 Subject: [PATCH] Added F (fortune) trigger; resolves #105 --- .../dungeonsxl/trigger/FortuneTrigger.java | 78 +++++++++++++++++++ .../dre2n/dungeonsxl/trigger/Trigger.java | 6 ++ .../trigger/TriggerTypeDefault.java | 1 + .../dre2n/dungeonsxl/world/DGameWorld.java | 5 ++ 4 files changed, 90 insertions(+) create mode 100644 src/main/java/io/github/dre2n/dungeonsxl/trigger/FortuneTrigger.java diff --git a/src/main/java/io/github/dre2n/dungeonsxl/trigger/FortuneTrigger.java b/src/main/java/io/github/dre2n/dungeonsxl/trigger/FortuneTrigger.java new file mode 100644 index 00000000..98ae9165 --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/trigger/FortuneTrigger.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2012-2016 Frank Baumann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.github.dre2n.dungeonsxl.trigger; + +import io.github.dre2n.commons.util.NumberUtil; +import io.github.dre2n.dungeonsxl.event.trigger.TriggerActionEvent; +import static io.github.dre2n.dungeonsxl.trigger.Trigger.plugin; +import io.github.dre2n.dungeonsxl.world.DGameWorld; + +/** + * @author Frank Baumann, Daniel Saukel + */ +public class FortuneTrigger extends Trigger { + + private TriggerType type = TriggerTypeDefault.FORTUNE; + + private double chance = 0; + + public FortuneTrigger(double chance) { + this.chance = chance; + } + + /* Getters and setters */ + public double getChance() { + return chance; + } + + /** + * @param chance + * the chance to set + */ + public void setChance(double chance) { + this.chance = chance; + } + + @Override + public TriggerType getType() { + return type; + } + + /* Actions */ + public void onTrigger() { + int random = NumberUtil.generateRandomInt(0, 100); + if (chance * 100 < random) { + return; + } + + TriggerActionEvent event = new TriggerActionEvent(this); + plugin.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return; + } + + setTriggered(true); + updateDSigns(); + } + + /* Statics */ + public static FortuneTrigger getOrCreate(String chance, DGameWorld gameWorld) { + return new FortuneTrigger(NumberUtil.parseDouble(chance)); + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/trigger/Trigger.java b/src/main/java/io/github/dre2n/dungeonsxl/trigger/Trigger.java index 8f40a684..ab912406 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/trigger/Trigger.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/trigger/Trigger.java @@ -138,6 +138,12 @@ public abstract class Trigger { trigger = new DistanceTrigger(dSign.getSign().getLocation()); } + } else if (type == TriggerTypeDefault.FORTUNE) { + + if (value != null) { + trigger = new FortuneTrigger(NumberUtil.parseDouble(value)); + } + } else if (type == TriggerTypeDefault.SIGN) { if (value != null) { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/trigger/TriggerTypeDefault.java b/src/main/java/io/github/dre2n/dungeonsxl/trigger/TriggerTypeDefault.java index 03d7d8e0..b461cde6 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/trigger/TriggerTypeDefault.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/trigger/TriggerTypeDefault.java @@ -22,6 +22,7 @@ package io.github.dre2n.dungeonsxl.trigger; public enum TriggerTypeDefault implements TriggerType { DISTANCE("D", DistanceTrigger.class), + FORTUNE("F", FortuneTrigger.class), INTERACT("I", InteractTrigger.class), MOB("M", MobTrigger.class), PROGRESS("P", ProgressTrigger.class), diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java index 60b85937..c5ae2048 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java @@ -30,6 +30,7 @@ import io.github.dre2n.dungeonsxl.sign.DSignType; import io.github.dre2n.dungeonsxl.sign.DSignTypeDefault; import io.github.dre2n.dungeonsxl.sign.MobSign; import io.github.dre2n.dungeonsxl.sign.StartSign; +import io.github.dre2n.dungeonsxl.trigger.FortuneTrigger; import io.github.dre2n.dungeonsxl.trigger.ProgressTrigger; import io.github.dre2n.dungeonsxl.trigger.RedstoneTrigger; import io.github.dre2n.dungeonsxl.trigger.Trigger; @@ -375,6 +376,10 @@ public class DGameWorld extends DInstanceWorld { ((RedstoneTrigger) trigger).onTrigger(); } + for (Trigger trigger : getTriggers(TriggerTypeDefault.FORTUNE)) { + ((FortuneTrigger) trigger).onTrigger(); + } + for (DSign dSign : dSigns) { if (dSign != null) { if (!dSign.hasTriggers()) {