Skip to content

Commit

Permalink
Check fire tick on Match load and start (#879)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick <[email protected]>
  • Loading branch information
CoWinkKeyDinkInc authored Sep 4, 2021
1 parent 9fa901e commit efc9aa5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
40 changes: 40 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/map/GameRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tc.oc.pgm.api.map;

public enum GameRule {
DO_DAYLIGHT_CYCLE("doDaylightCycle"),
DO_FIRE_TICK("doFireTick"),
DO_MOB_LOOT("doMobLoot"),
DO_TILE_DROPS("doTileDrops"),
MOB_GRIEFING("mobGriefing"),
NATURAL_REGENERATION("naturalRegeneration");

/* Unsupported Gamerules:
doMobSpawning
keepInventory
commandBlockOutput
logAdminCommands
randomTickSpeed
reducedDebugInfo
sendCommandFeedback
showDeathMessages
*/

private final String id;

GameRule(String id) {
this.id = id;
}

public static GameRule byId(String gameRuleId) {
for (GameRule gameRule : GameRule.values()) {
if (gameRule.getId().equals(gameRuleId)) {
return gameRule;
}
}
return null;
}

public String getId() {
return id;
}
}
20 changes: 17 additions & 3 deletions core/src/main/java/tc/oc/pgm/gamerules/GameRulesMatchModule.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tc.oc.pgm.gamerules;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import tc.oc.pgm.api.map.GameRule;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchModule;
import tc.oc.pgm.modules.WorldTimeModule;

public class GameRulesMatchModule implements MatchModule {

Expand All @@ -18,12 +19,25 @@ public GameRulesMatchModule(Match match, Map<String, String> gameRules) {

@Override
public void load() {
// saves and sets gamerules from XML
for (Map.Entry<String, String> gameRule : this.gameRules.entrySet()) {
gameRules.put(gameRule.getKey(), gameRule.getValue());

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 6, 2021

Contributor

This line does nothing.

This comment has been minimized.

Copy link
@CoWinkKeyDinkInc

CoWinkKeyDinkInc Sep 7, 2021

Author Contributor

This still sets gamerules mentioned in XML like naturalRegeneration and so on

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 7, 2021

Contributor

this.gameRules and gameRules are the same object. You're iterating over a map's entries and then setting the key equal to the entry value. The gamerules in the XML are set by the module factory. See

for (Element gameRulesElement : doc.getRootElement().getChildren("gamerules")) {

this.match.getWorld().setGameRuleValue(gameRule.getKey(), gameRule.getValue());
}

// saves gamerules from world (level.dat) as fallback
for (String gameRule : this.match.getWorld().getGameRules()) {

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 6, 2021

Contributor

This causes PGM to use level.dat's doDaylightCycle.

This comment has been minimized.

Copy link
@CoWinkKeyDinkInc

CoWinkKeyDinkInc Sep 7, 2021

Author Contributor

putIfAbsent stops this if <doDaylightCycle> is already in the XML. This gets overwritten by the WorldTimeModule's default off setting for daylightcycle. Originally there was an if (!gamerule.equals("doDaylightCycle") in there but Pablo wanted that removed.

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 7, 2021

Contributor

This gets overwritten by the WorldTimeModule's default off setting for daylightcycle

only if time isn't locked.

gameRules.putIfAbsent(gameRule, this.match.getWorld().getGameRuleValue(gameRule));
}

// if timelock is off, save doDayLightCycle as true
WorldTimeModule wtm = this.match.getModule(WorldTimeModule.class);
if (wtm != null && !wtm.isTimeLocked()) {
gameRules.put(GameRule.DO_DAYLIGHT_CYCLE.getId(), Boolean.toString(true));

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 7, 2021

Contributor
gameRules.put(GameRule.DO_DAYLIGHT_CYCLE.getId(), Boolean.toString(wtm != null && !wtm.isTimeLocked()));

Move it above the for on line 29 & remove the if on line 35 too. That should fix level.dat's doDaylightCycle overriding PGM's.

}
}

public ImmutableMap<String, String> getGameRules() {
return ImmutableMap.copyOf(gameRules);
public String getGameRule(String gameRule) {
return gameRules.get(gameRule);
}
}
52 changes: 44 additions & 8 deletions core/src/main/java/tc/oc/pgm/listeners/PGMListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.Permissions;
import tc.oc.pgm.api.event.BlockTransformEvent;
import tc.oc.pgm.api.map.GameRule;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchManager;
import tc.oc.pgm.api.match.event.MatchFinishEvent;
Expand All @@ -60,7 +61,6 @@
import tc.oc.pgm.util.text.TextTranslations;

public class PGMListener implements Listener {
private static final String DO_DAYLIGHT_CYCLE = "doDaylightCycle";
/*
1000 /time set day
6000 noon, sun is at its peak
Expand Down Expand Up @@ -258,31 +258,59 @@ public void handleItemPickup(final PlayerPickupItemEvent event) {
if (nearestPlayer != event.getPlayer()) event.setCancelled(true);
}

@EventHandler
public void lockFireTick(final MatchLoadEvent event) {
setGameRule(event, GameRule.DO_FIRE_TICK.getId(), false);
}

@EventHandler
public void unlockFireTick(final MatchStartEvent event) {
event
.getMatch()
.getWorld()
.setGameRuleValue(
GameRule.DO_FIRE_TICK.getId(),
event
.getMatch()
.needModule(GameRulesMatchModule.class)
.getGameRule(GameRule.DO_FIRE_TICK.getId()));
}

@EventHandler
public void lockFireTick(final MatchFinishEvent event) {
setGameRule(event, GameRule.DO_DAYLIGHT_CYCLE.getId(), false);

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 6, 2021

Contributor

Wrong gamerule.

}

//
// Time Lock
// lock time before, during (if time lock enabled), and after the match
//
@EventHandler
public void lockTime(final MatchLoadEvent event) {
event.getMatch().getWorld().setGameRuleValue(DO_DAYLIGHT_CYCLE, Boolean.toString(false));
setGameRule(event, GameRule.DO_DAYLIGHT_CYCLE.getId(), false);
}

@EventHandler
public void unlockTime(final MatchStartEvent event) {
// if there is a timelock module and it is off, unlock time
boolean unlockTime = !event.getMatch().getModule(WorldTimeModule.class).isTimeLocked();

This comment has been minimized.

Copy link
@dentmaged

dentmaged Sep 7, 2021

Contributor

This shouldn't be needed as GameRulesMatchModule#load has already set the value of doDaylightCycle using the WorldTimeModule.


GameRulesMatchModule gameRulesModule = event.getMatch().getModule(GameRulesMatchModule.class);
if (gameRulesModule != null && gameRulesModule.getGameRules().containsKey(DO_DAYLIGHT_CYCLE)) {
unlockTime = Boolean.parseBoolean(gameRulesModule.getGameRules().get(DO_DAYLIGHT_CYCLE));
GameRulesMatchModule gameRulesMatchModule =
event.getMatch().getModule(GameRulesMatchModule.class);
if (gameRulesMatchModule != null
&& Boolean.parseBoolean(
gameRulesMatchModule.getGameRule(GameRule.DO_DAYLIGHT_CYCLE.getId()))) {
unlockTime = true;
}

event.getMatch().getWorld().setGameRuleValue(DO_DAYLIGHT_CYCLE, Boolean.toString(unlockTime));
event
.getMatch()
.getWorld()
.setGameRuleValue(GameRule.DO_DAYLIGHT_CYCLE.getId(), Boolean.toString(unlockTime));
}

@EventHandler
public void lockTime(final MatchFinishEvent event) {
event.getMatch().getWorld().setGameRuleValue(DO_DAYLIGHT_CYCLE, Boolean.toString(false));
setGameRule(event, GameRule.DO_DAYLIGHT_CYCLE.getId(), false);
}

@EventHandler
Expand Down Expand Up @@ -405,4 +433,12 @@ public void storeSkinOnMatchJoin(PlayerJoinMatchEvent event) {
final MatchPlayer player = event.getPlayer();
PGM.get().getDatastore().setSkin(player.getId(), NMSHacks.getPlayerSkin(player.getBukkit()));
}

public void setGameRule(MatchLoadEvent event, String gameRule, boolean gameRuleValue) {
event.getMatch().getWorld().setGameRuleValue(gameRule, Boolean.toString(gameRuleValue));
}

public void setGameRule(MatchFinishEvent event, String gameRule, boolean gameRuleValue) {
event.getMatch().getWorld().setGameRuleValue(gameRule, Boolean.toString(gameRuleValue));
}
}

0 comments on commit efc9aa5

Please sign in to comment.