Skip to content

Commit

Permalink
Adds real time carbon score alerts for players
Browse files Browse the repository at this point in the history
Optin alerts, permission required.
  • Loading branch information
nsporillo committed May 17, 2019
1 parent 93f9790 commit 064df62
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/porillo/commands/GeneralCommands.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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("")
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/porillo/config/Lang.java
Expand Up @@ -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(""),
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/net/porillo/engine/api/WorldClimateEngine.java
Expand Up @@ -50,15 +50,24 @@ public WorldClimateEngine(WorldConfig config) {

public Reduction treeGrow(Tree tree, List<BlockState> 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++;
}
}
}

Expand All @@ -69,6 +78,7 @@ public Reduction treeGrow(Tree tree, List<BlockState> blocks, boolean bonemealUs
reduction.setReductioner(tree.getOwnerId());
reduction.setReductionKey(tree.getUniqueId());
reduction.setReductionValue(reductionValue);
reduction.setNumBlocks(numBlocks);
return reduction;
}

Expand Down
7 changes: 7 additions & 0 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/porillo/objects/Reduction.java
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions 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<UUID> 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;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/lang.yml
Expand Up @@ -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
Expand Down

0 comments on commit 064df62

Please sign in to comment.