diff --git a/src/main/java/net/porillo/commands/GeneralCommands.java b/src/main/java/net/porillo/commands/GeneralCommands.java index cee6f95..06a4920 100644 --- a/src/main/java/net/porillo/commands/GeneralCommands.java +++ b/src/main/java/net/porillo/commands/GeneralCommands.java @@ -11,6 +11,7 @@ import net.porillo.engine.models.CarbonIndexModel; import net.porillo.objects.GPlayer; import net.porillo.objects.OffsetBounty; +import net.porillo.util.AlertManager; import net.porillo.util.ChatTable; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -167,6 +168,22 @@ public void onShow(GPlayer gPlayer) { } } + @Subcommand("alerts") + @Description("Sends message alerts on all carbon activities") + @Syntax("") + @CommandPermission("globalwarming.score.alerts") + public void onAlert(GPlayer gPlayer) { + if (isCommandAllowed(gPlayer)) { + if (!AlertManager.getInstance().isSubscribed(gPlayer.getUuid())) { + AlertManager.getInstance().subscribe(gPlayer.getUuid()); + gPlayer.sendMsg(Lang.ALERT_SUBSCRIBE.get()); + } else { + AlertManager.getInstance().unsubscribe(gPlayer.getUuid()); + gPlayer.sendMsg(Lang.ALERT_UNSUBSCRIBE.get()); + } + } + } + @Subcommand("hide") @Description("Hide the scoreboard") @Syntax("") diff --git a/src/main/java/net/porillo/config/Lang.java b/src/main/java/net/porillo/config/Lang.java index 8c09764..01413ff 100644 --- a/src/main/java/net/porillo/config/Lang.java +++ b/src/main/java/net/porillo/config/Lang.java @@ -11,6 +11,12 @@ public enum Lang { /** * See lang.yml for translations */ + ALERT_SUBSCRIBE(""), + ALERT_UNSUBSCRIBE(""), + ALERT_TREEREDUCE(""), + ALERT_TREEREDUCEBONEMEAL(""), + ALERT_BURNCONTRIB(""), + ALERT_FARMCONTRIB(""), BOUNTY_PLAYER(""), BOUNTY_HUNTER(""), BOUNTY_BLOCKS(""), diff --git a/src/main/java/net/porillo/engine/api/WorldClimateEngine.java b/src/main/java/net/porillo/engine/api/WorldClimateEngine.java index c4f865b..10dbeb3 100644 --- a/src/main/java/net/porillo/engine/api/WorldClimateEngine.java +++ b/src/main/java/net/porillo/engine/api/WorldClimateEngine.java @@ -50,15 +50,24 @@ public WorldClimateEngine(WorldConfig config) { public Reduction treeGrow(Tree tree, List blocks, boolean bonemealUsed) { int reductionValue = 0; + int numBlocks = 0; if (bonemealUsed && !config.isBonemealReductionAllowed()) { return null; } else if (bonemealUsed) { for (BlockState bs : blocks) { - reductionValue += (config.getBonemealReductionModifier() * reductionModel.getReduction(bs.getType())); + double reduction = reductionModel.getReduction(bs.getType()); + if (reduction > 0.0) { + reductionValue += (config.getBonemealReductionModifier() * reduction); + numBlocks++; + } } } else { for (BlockState bs : blocks) { - reductionValue += reductionModel.getReduction(bs.getType()); + double reduction = reductionModel.getReduction(bs.getType()); + if (reduction > 0.0) { + reductionValue += reduction; + numBlocks++; + } } } @@ -69,6 +78,7 @@ public Reduction treeGrow(Tree tree, List blocks, boolean bonemealUs reduction.setReductioner(tree.getOwnerId()); reduction.setReductionKey(tree.getUniqueId()); reduction.setReductionValue(reductionValue); + reduction.setNumBlocks(numBlocks); return reduction; } diff --git a/src/main/java/net/porillo/listeners/CH4Listener.java b/src/main/java/net/porillo/listeners/CH4Listener.java index eb54db4..fe70064 100644 --- a/src/main/java/net/porillo/listeners/CH4Listener.java +++ b/src/main/java/net/porillo/listeners/CH4Listener.java @@ -1,6 +1,7 @@ package net.porillo.listeners; import net.porillo.GlobalWarming; +import net.porillo.config.Lang; import net.porillo.database.queries.insert.ContributionInsertQuery; import net.porillo.database.queries.insert.EntityInsertQuery; import net.porillo.database.queries.update.PlayerUpdateQuery; @@ -12,6 +13,7 @@ import net.porillo.objects.Contribution; import net.porillo.objects.GPlayer; import net.porillo.objects.TrackedEntity; +import net.porillo.util.AlertManager; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -97,6 +99,11 @@ public void onEntityDeath(EntityDeathEvent event) { ContributionInsertQuery insertQuery = new ContributionInsertQuery(contribution); AsyncDBQueue.getInstance().queueInsertQuery(insertQuery); contributionValue = contribution.getContributionValue(); + + // Execute real time player notification if they're subscribed with /gw score alerts + AlertManager.getInstance().alert(polluter, + String.format(Lang.ALERT_FARMCONTRIB.get(), + entity.getEntityType().name().toLowerCase(), contribution.getContributionValue())); } //Polluter carbon scores: diff --git a/src/main/java/net/porillo/listeners/CO2Listener.java b/src/main/java/net/porillo/listeners/CO2Listener.java index 391826d..127ae34 100644 --- a/src/main/java/net/porillo/listeners/CO2Listener.java +++ b/src/main/java/net/porillo/listeners/CO2Listener.java @@ -1,6 +1,7 @@ package net.porillo.listeners; import net.porillo.GlobalWarming; +import net.porillo.config.Lang; import net.porillo.database.queries.insert.ContributionInsertQuery; import net.porillo.database.queries.insert.FurnaceInsertQuery; import net.porillo.database.queries.insert.ReductionInsertQuery; @@ -14,6 +15,7 @@ import net.porillo.engine.ClimateEngine; import net.porillo.engine.api.WorldClimateEngine; import net.porillo.objects.*; +import net.porillo.util.AlertManager; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.event.EventHandler; @@ -109,6 +111,11 @@ public void onFurnaceSmelt(FurnaceBurnEvent event) { ContributionInsertQuery insertQuery = new ContributionInsertQuery(contribution); AsyncDBQueue.getInstance().queueInsertQuery(insertQuery); contributionValue = contribution.getContributionValue(); + + // Execute real time player notification if they're subscribed with /gw score alerts + AlertManager.getInstance().alert(polluter, + String.format(Lang.ALERT_BURNCONTRIB.get(), event.getFuel().getType().name().toLowerCase(), + contribution.getContributionValue())); } //Polluter carbon scores: @@ -232,6 +239,17 @@ public void onStructureGrow(StructureGrowEvent event) { AsyncDBQueue.getInstance().queueUpdateQuery(updateQuery); } + // Execute real time player notification if they're subscribed with /gw score alerts + if (event.isFromBonemeal()) { + AlertManager.getInstance().alert(planter, + String.format(Lang.ALERT_TREEREDUCE.get(), + reduction.getNumBlocks(), reduction.getReductionValue())); + } else { + AlertManager.getInstance().alert(planter, + String.format(Lang.ALERT_TREEREDUCEBONEMEAL.get(), + reduction.getNumBlocks(), reduction.getReductionValue())); + } + //Update the affected world's carbon levels: GlobalWarming.getInstance().getTableManager().getWorldTable().updateWorldCarbonValue(affectedWorldId, -reductionValue); diff --git a/src/main/java/net/porillo/objects/Reduction.java b/src/main/java/net/porillo/objects/Reduction.java index 8b814b0..baf4aef 100644 --- a/src/main/java/net/porillo/objects/Reduction.java +++ b/src/main/java/net/porillo/objects/Reduction.java @@ -37,6 +37,11 @@ public class Reduction { */ private Integer reductionValue; + /** + * Number of blocks in the tree growth, if this reduction is a tree growth + */ + private Integer numBlocks; + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/net/porillo/util/AlertManager.java b/src/main/java/net/porillo/util/AlertManager.java new file mode 100644 index 0000000..c8804fe --- /dev/null +++ b/src/main/java/net/porillo/util/AlertManager.java @@ -0,0 +1,37 @@ +package net.porillo.util; + +import net.porillo.objects.GPlayer; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +public class AlertManager { + + private static AlertManager instance; // single instance + + private Set subscribers = new HashSet<>(); + + public void subscribe(UUID uuid) { + this.subscribers.add(uuid); + } + + public void unsubscribe(UUID uuid) { + this.subscribers.remove(uuid); + } + + public boolean isSubscribed(UUID uuid) { + return subscribers.contains(uuid); + } + + public void alert(GPlayer player, String message) { + if (subscribers.contains(player.getUuid())) { + player.sendMsg(message); + } + } + + public static AlertManager getInstance() { + if (instance == null) instance = new AlertManager(); + return instance; + } +} diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index ce4d4cb..89800b2 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -5,6 +5,13 @@ lang: INVALIDARGS: '§c§OInvalid command arguments, expecting: §7§O%s' SPAM: §8§OToo many requests at once INVENTORYFULL: §c§OYour inventory is full + ALERT: + SUBSCRIBE: '§2Subscribed to real time carbon score alerts' + UNSUBSCRIBE: '§cUnsubscribed from real time carbon score alerts' + TREEREDUCE: '§2[Alert] Tree grown. §f(§ablocks: %d§f, §breduction: %d§f)' + TREEREDUCEBONEMEAL: '§2[Alert] Tree grown with §cbonemeal. §f(§ablocks: %d§f, §b%reduction: %d§f)' + BURNCONTRIB: '§c[Alert] §O%s burned. §4(contribution: %d)' + FARMCONTRIB: '§c[Alert] §c§OBred %s killed. §4(contribution: %d )' BOUNTY: PLAYER: PLAYER HUNTER: HUNTER