Skip to content

Commit

Permalink
Added F (fortune) trigger; resolves #105
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Jul 5, 2016
1 parent cd0fe34 commit 2389d54
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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));
}

}
6 changes: 6 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/trigger/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public static Trigger getOrCreate(String identifier, String value, DSign dSign)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -375,6 +376,10 @@ public void startGame() {
((RedstoneTrigger) trigger).onTrigger();
}

for (Trigger trigger : getTriggers(TriggerTypeDefault.FORTUNE)) {
((FortuneTrigger) trigger).onTrigger();
}

for (DSign dSign : dSigns) {
if (dSign != null) {
if (!dSign.hasTriggers()) {
Expand Down

0 comments on commit 2389d54

Please sign in to comment.