Skip to content

Commit

Permalink
docs: add javadocs for GameRules
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Sep 15, 2024
1 parent 49d242e commit 02f95cf
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Arrays;

/**
* Allay Project 2023/3/4
* GameRule represents a game rule in the game
*
* @author Jukebox | Cool_Loong
*/
Expand Down Expand Up @@ -52,25 +52,19 @@ public enum GameRule {
private final Object defaultValue;
private final Type type;

/**
* Get a game rule by name.
*
* @param name the name of the game rule
* @return the game rule, or {@code null} if not found
*/
public static GameRule fromName(String name) {
return Arrays.stream(values())
.filter(gameRule -> gameRule.getName().equalsIgnoreCase(name))
.findFirst()
.orElse(null);
}

public static boolean parseByteToBoolean(byte value) {
return value == 1;
}

public GameRuleData<?> toNetwork() {
return new GameRuleData<>(name, defaultValue);
}

public <T> GameRuleData<T> toNetwork(T value) {
return new GameRuleData<>(name, value);
}

public enum Type {
INT,
BOOLEAN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.stream.Collectors;

/**
* Allay Project 2023/3/4
* GameRules is a container for storing and managing game rules.
*
* @author Jukebox | Cool_Loong
*/
Expand All @@ -23,18 +23,35 @@ public class GameRules {

private final Map<GameRule, Object> gameRules = new HashMap<>();

/**
* Whether the game rules have been modified since last sending to the players.
*/
private boolean dirty;

/**
* Creates a new GameRules instance with default values.
*/
public GameRules() {
for (GameRule gameRule : GameRule.values()) {
this.gameRules.put(gameRule, gameRule.getDefaultValue());
}
}

/**
* Creates a new GameRules instance with the specified game rules.
*
* @param gameRules the game rules to be added
*/
public GameRules(Map<GameRule, Object> gameRules) {
this.gameRules.putAll(gameRules);
}

/**
* Reads game rules from NBT.
*
* @param nbt the NBT to read from
* @return the game rules
*/
public static GameRules readFromNBT(NbtMap nbt) {
Map<GameRule, Object> gameRules = new HashMap<>();
for (GameRule gameRule : GameRule.values()) {
Expand All @@ -49,39 +66,77 @@ public static GameRules readFromNBT(NbtMap nbt) {
return new GameRules(gameRules);
}

/**
* Get the game rules.
*
* @return the game rules
*/
@UnmodifiableView
public Map<GameRule, Object> getGameRules() {
return Collections.unmodifiableMap(gameRules);
}

public void put(GameRule gameRule, Object o) {
gameRules.put(gameRule, o);
/**
* Set a game rule's value.
*
* @param gameRule the game rule
* @param value the value
*/
public void put(GameRule gameRule, Object value) {
gameRules.put(gameRule, value);
dirty = true;
}

/**
* Get a game rule's value.
*
* @param gameRule the game rule
* @param <V> the type of the value
* @return the value
*/
@SuppressWarnings("unchecked")
public <V> V get(GameRule gameRule) {
return (V) gameRules.getOrDefault(gameRule, gameRule.getDefaultValue());
}

/**
* Send the game rules to the players in a world.
*
* @param world the world which players are in.
*/
public void sync(World world) {
if (!dirty) return;
world.broadcastPacket(buildPacket());
dirty = false;
}

/**
* Builds a GameRulesChangedPacket from the game rules.
*
* @return the packet
*/
public GameRulesChangedPacket buildPacket() {
var pk = new GameRulesChangedPacket();
pk.getGameRules().addAll(toNetworkGameRuleData());
return pk;
}

/**
* Converts the game rules to a list of GameRuleData.
*
* @return the list of GameRuleData
*/
public List<GameRuleData<?>> toNetworkGameRuleData() {
return this.getGameRules().entrySet().stream()
.map(entry -> new GameRuleData<>(entry.getKey().getName(), entry.getValue()))
.collect(Collectors.toList());
}

/**
* Writes the game rules to NBT.
*
* @param builder the NbtMapBuilder to write to
*/
public void writeToNBT(NbtMapBuilder builder) {
for (Map.Entry<GameRule, Object> entry : this.gameRules.entrySet()) {
// Lower case name should be used for key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class EntitySpawnContext extends Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class LightContext extends Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class NoiseContext extends Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public abstract class OtherChunkAccessibleContext extends Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class PopulateContext extends OtherChunkAccessibleContext {
Expand Down

0 comments on commit 02f95cf

Please sign in to comment.