-
Notifications
You must be signed in to change notification settings - Fork 46
/
CH4Listener.java
129 lines (108 loc) · 5.58 KB
/
CH4Listener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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;
import net.porillo.database.queue.AsyncDBQueue;
import net.porillo.database.tables.EntityTable;
import net.porillo.database.tables.PlayerTable;
import net.porillo.engine.ClimateEngine;
import net.porillo.engine.api.WorldClimateEngine;
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;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityBreedEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import java.util.UUID;
public class CH4Listener implements Listener {
private GlobalWarming gw;
public CH4Listener(GlobalWarming gw) {
this.gw = gw;
}
@EventHandler
public void onEntityBreed(EntityBreedEvent event) {
//Ignore if the entities world-climate is disabled:
UUID worldId = event.getMother().getWorld().getUID();
WorldClimateEngine eventClimateEngine = ClimateEngine.getInstance().getClimateEngine(worldId);
if (eventClimateEngine == null || !eventClimateEngine.isEnabled()) {
return;
}
PlayerTable playerTable = gw.getTableManager().getPlayerTable();
LivingEntity breeder = event.getBreeder();
if (breeder instanceof Player) {
Player player = (Player) breeder;
GPlayer gPlayer = playerTable.getOrCreatePlayer(player.getUniqueId());
// Track mother, father, and child. Associate the player breeder to them.
trackEntity(event.getMother(), gPlayer);
trackEntity(event.getFather(), gPlayer);
trackEntity(event.getEntity(), gPlayer);
}
}
private void trackEntity(LivingEntity entity, GPlayer gPlayer) {
EntityTable entityTable = gw.getTableManager().getEntityTable();
if (!entityTable.getEntityMap().containsKey(entity.getUniqueId())) {
int uniqueId = GlobalWarming.getInstance().getRandom().nextInt(Integer.MAX_VALUE);
TrackedEntity trackedEntity = new TrackedEntity();
trackedEntity.setUniqueId(uniqueId);
trackedEntity.setUuid(entity.getUniqueId());
trackedEntity.setBreederId(gPlayer.getUniqueId());
trackedEntity.setEntityType(entity.getType());
trackedEntity.setTicksLived(entity.getTicksLived());
trackedEntity.setAlive(!entity.isDead());
entityTable.updateCollections(trackedEntity);
EntityInsertQuery insertQuery = new EntityInsertQuery(trackedEntity);
AsyncDBQueue.getInstance().queueInsertQuery(insertQuery);
}
}
@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
//Ignore if the entities world-climate is disabled:
UUID worldId = event.getEntity().getWorld().getUID();
WorldClimateEngine eventClimateEngine = ClimateEngine.getInstance().getClimateEngine(worldId);
if (eventClimateEngine == null || !eventClimateEngine.isEnabled()) {
return;
}
EntityTable entityTable = gw.getTableManager().getEntityTable();
if (entityTable.getEntityMap().containsKey(event.getEntity().getUniqueId())) {
TrackedEntity entity = entityTable.getEntityMap().get(event.getEntity().getUniqueId());
entity.setTicksLived(event.getEntity().getTicksLived());
entity.setAlive(false);
int breederID = entity.getBreederId();
PlayerTable playerTable = gw.getTableManager().getPlayerTable();
GPlayer polluter = playerTable.getOrCreatePlayer(playerTable.getUuidMap().get(breederID));
int contributionValue = 0;
Contribution contribution = eventClimateEngine.methaneRelease(entity);
if (contribution != null) {
//Queue an insert into the contributions table:
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:
if (polluter != null) {
//Increment the polluter's carbon score:
int carbonScore = polluter.getCarbonScore();
polluter.setCarbonScore(carbonScore + contributionValue);
//Queue an update to the player table:
PlayerUpdateQuery updateQuery = new PlayerUpdateQuery(polluter);
AsyncDBQueue.getInstance().queueUpdateQuery(updateQuery);
}
//Update the affected world's carbon levels:
gw.getTableManager().getWorldTable().updateWorldCarbonValue(worldId, contributionValue);
//Update the scoreboard:
gw.getScoreboard().update(polluter);
// Untrack the dead entity
entityTable.delete(event.getEntity().getUniqueId(), false);
}
}
}