Skip to content

Commit 86ca4ea

Browse files
committed
add: food, saturation & health state
1 parent 8221ca2 commit 86ca4ea

9 files changed

Lines changed: 70 additions & 25 deletions

File tree

build.gradle

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
plugins {
22
id 'java'
33
id("xyz.jpenilla.run-paper") version "2.3.1"
4-
id "com.github.johnrengelman.shadow" version "8.1.1"
4+
id("com.gradleup.shadow") version "9.3.1"
55
}
66

77
group = 'tech.techstreet.border'
8-
version = '2.0.0'
8+
version = '2.0.1'
99
build.dependsOn shadowJar
1010

1111
repositories {
@@ -19,6 +19,7 @@ repositories {
1919
dependencies {
2020
implementation("cloud.commandframework:cloud-paper:1.5.0") // Cloud Paper Framework
2121
implementation("cloud.commandframework:cloud-minecraft-extras:1.5.0") // Cloud Extras
22+
implementation("org.bstats:bstats-bukkit:3.1.0") // bStats Metrics
2223

2324
compileOnly("io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT")
2425
}
@@ -42,13 +43,14 @@ java {
4243
}
4344
}
4445

45-
tasks.withType(Jar).configureEach {
46-
from("LICENSE") {
47-
into("")
48-
}
49-
}
46+
tasks.shadowJar {
47+
archiveBaseName.set(project.name)
48+
archiveClassifier.set("all")
49+
50+
relocate("org.bstats", "tech.techstreet.border.shadow.bstats")
51+
from("LICENSE") { into("") }
52+
from(sourceSets.main.output)
5053

51-
tasks.withType(Jar).configureEach {
5254
manifest {
5355
attributes(
5456
"Implementation-Title": project.name,
@@ -57,6 +59,8 @@ tasks.withType(Jar).configureEach {
5759
"License-URL": "https://www.gnu.org/licenses/agpl-3.0.html"
5860
)
5961
}
62+
63+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
6064
}
6165

6266
tasks.withType(JavaCompile).configureEach {

src/main/java/tech/techstreet/border/BorderHoarderPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package tech.techstreet.border;
1818

1919
import net.kyori.adventure.text.Component;
20+
import org.bstats.bukkit.Metrics;
2021
import org.bukkit.Bukkit;
2122
import org.bukkit.plugin.java.JavaPlugin;
2223
import tech.techstreet.border.command.CommandHandler;
@@ -27,7 +28,6 @@
2728
public final class BorderHoarderPlugin extends JavaPlugin {
2829
private static BorderHoarderPlugin instance;
2930
private static BorderHandler borderHandler;
30-
3131
private static final String MINECRAFT_VERSION = "1.21.11";
3232

3333
/**
@@ -45,8 +45,10 @@ public void onEnable() {
4545
Bukkit.getConsoleSender().sendMessage(Component.text("[" + getPluginName() + "] This plugin supports Minecraft " + MINECRAFT_VERSION + " only, you are running " + Bukkit.getServer().getMinecraftVersion(), Colours.RED));
4646
}
4747

48+
Bukkit.motd(Component.text("Border Hoarder v" + this.getDescription().getVersion()));
4849
CommandHandler commandHandler = new CommandHandler(instance);
4950
EventHandler eventHandler = new EventHandler(instance);
51+
Metrics metrics = new Metrics(this, 29626);
5052

5153
commandHandler.load();
5254
eventHandler.load();

src/main/java/tech/techstreet/border/events/player/PlayerConnectionEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import tech.techstreet.border.lib.user.User;
3030
import tech.techstreet.border.lib.user.UserManager;
3131
import tech.techstreet.border.lib.user.UserState;
32+
import tech.techstreet.border.lib.user.UserStats;
3233

3334
public class PlayerConnectionEvent implements Listener {
3435

@@ -67,7 +68,7 @@ public void onEvent(PlayerJoinEvent event) {
6768
User user = UserManager.of(player);
6869

6970
// Send player back to previous state, but default to the lobby
70-
user.setState(ProgressHandler.getLastStates().getOrDefault(player.getUniqueId(), UserState.LOBBY));
71+
user.setState(ProgressHandler.getLastStats().getOrDefault(player.getUniqueId(), new UserStats(UserState.LOBBY, 20, 20, 20)).state());
7172
}
7273

7374
/**

src/main/java/tech/techstreet/border/events/player/PlayerDamageEvent.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class PlayerDamageEvent implements Listener {
2929

3030
/**
3131
* Prevents players in the LOBBY state from taking damage.
32-
*
3332
* @param event The EntityDamageEvent to handle.
3433
*/
3534
@EventHandler
@@ -44,7 +43,6 @@ public void onEvent(EntityDamageEvent event) {
4443

4544
/**
4645
* Prevents players in the LOBBY state from losing hunger.
47-
*
4846
* @param event The FoodLevelChangeEvent to handle.
4947
*/
5048
@EventHandler

src/main/java/tech/techstreet/border/lib/border/BorderHandler.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import tech.techstreet.border.lib.user.User;
3434
import tech.techstreet.border.lib.user.UserManager;
3535
import tech.techstreet.border.lib.user.UserState;
36+
import tech.techstreet.border.lib.user.UserStats;
3637

3738
import java.util.Arrays;
3839
import java.util.HashMap;
@@ -85,9 +86,18 @@ public void load() {
8586
User user = UserManager.of(player);
8687

8788
if (user.getState() == UserState.PLAY) {
88-
ProgressHandler.updateState(player.getUniqueId(), player.getLocation(), UserState.PLAY);
89+
ProgressHandler.updateState(player.getUniqueId(), player.getLocation(), new UserStats(UserState.PLAY, player.getFoodLevel(), player.getSaturation(), player.getHealth()));
8990
} else {
90-
ProgressHandler.updateState(player.getUniqueId(), ProgressHandler.getLastLocations().get(player.getUniqueId()), UserState.LOBBY);
91+
ProgressHandler.updateState(
92+
player.getUniqueId(),
93+
ProgressHandler.getLastLocations().get(player.getUniqueId()),
94+
new UserStats(
95+
UserState.LOBBY,
96+
ProgressHandler.getLastStats().get(player.getUniqueId()).food(),
97+
ProgressHandler.getLastStats().get(player.getUniqueId()).saturation(),
98+
ProgressHandler.getLastStats().get(player.getUniqueId()).health()
99+
)
100+
);
91101
}
92102
}
93103

@@ -124,11 +134,20 @@ public void saveLocation(Player player) {
124134
User user = UserManager.of(player);
125135

126136
if (user.getState() == UserState.PLAY) {
127-
ProgressHandler.updateState(player.getUniqueId(), player.getLocation(), UserState.PLAY);
137+
ProgressHandler.updateState(player.getUniqueId(), player.getLocation(), new UserStats(UserState.PLAY, player.getFoodLevel(), player.getSaturation(), player.getHealth()));
128138
}
129139

130140
if (user.getState() == UserState.LOBBY) {
131-
ProgressHandler.updateState(player.getUniqueId(), ProgressHandler.getLastLocations().get(player.getUniqueId()), UserState.LOBBY);
141+
ProgressHandler.updateState(
142+
player.getUniqueId(),
143+
ProgressHandler.getLastLocations().get(player.getUniqueId()),
144+
new UserStats(
145+
UserState.LOBBY,
146+
ProgressHandler.getLastStats().get(player.getUniqueId()).food(),
147+
ProgressHandler.getLastStats().get(player.getUniqueId()).saturation(),
148+
ProgressHandler.getLastStats().get(player.getUniqueId()).health()
149+
)
150+
);
132151
}
133152

134153
ProgressHandler.saveWorld(completedItems);

src/main/java/tech/techstreet/border/lib/border/ProgressHandler.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bukkit.Location;
2525
import tech.techstreet.border.lib.item.BoarderItem;
2626
import tech.techstreet.border.lib.user.UserState;
27+
import tech.techstreet.border.lib.user.UserStats;
2728

2829
import java.io.ByteArrayInputStream;
2930
import java.io.ByteArrayOutputStream;
@@ -38,7 +39,7 @@
3839
public class ProgressHandler {
3940
private static final File file = new File("world/data/border.dat");
4041
private static final HashMap<UUID, Location> lastLocations = new HashMap<>();
41-
private static final HashMap<UUID, UserState> lastStates = new HashMap<>();
42+
private static final HashMap<UUID, UserStats> lastStats = new HashMap<>();
4243

4344
/**
4445
* Gzip and Base64 encode a JSON string.
@@ -103,7 +104,10 @@ public static void saveWorld(List<BoarderItem> completedItems) {
103104
location.addProperty("z", lastLocations.get(uuid).getZ());
104105
location.addProperty("pitch", lastLocations.get(uuid).getPitch());
105106
location.addProperty("yaw", lastLocations.get(uuid).getYaw());
106-
location.addProperty("state", lastStates.get(uuid).name());
107+
location.addProperty("state", lastStats.get(uuid).state().name());
108+
location.addProperty("food", lastStats.get(uuid).food());
109+
location.addProperty("saturation", lastStats.get(uuid).saturation());
110+
location.addProperty("health", lastStats.get(uuid).health());
107111

108112
locations.add(uuid.toString(), location);
109113
}
@@ -141,7 +145,13 @@ public static List<BoarderItem> loadWorld() throws Exception {
141145
if (locations != null) {
142146
for (String uuid : locations.keySet()) {
143147
try {
144-
lastStates.put(UUID.fromString(uuid), UserState.valueOf(locations.getAsJsonObject(uuid).get("state").getAsString()));
148+
JsonObject userData = locations.getAsJsonObject(uuid);
149+
UserState state = UserState.valueOf(userData.get("state").getAsString());
150+
int food = userData.has("food") ? userData.get("food").getAsInt() : 20;
151+
float saturation = userData.has("saturation") ? userData.get("saturation").getAsFloat() : 20f;
152+
double health = userData.has("health") ? userData.get("health").getAsFloat() : 20d;
153+
154+
lastStats.put(UUID.fromString(uuid), new UserStats(state, food, saturation, health));
145155
lastLocations.put(UUID.fromString(uuid), new Location(
146156
Bukkit.getWorld(locations.getAsJsonObject(uuid).get("world").getAsString()),
147157
locations.getAsJsonObject(uuid).get("x").getAsDouble(),
@@ -163,20 +173,20 @@ public static List<BoarderItem> loadWorld() throws Exception {
163173
*
164174
* @param uuid the UUID of the user.
165175
* @param location the last known location of the user.
166-
* @param state the last known state of the user.
176+
* @param stats the last known stats of the user.
167177
*/
168-
public static void updateState(UUID uuid, Location location, UserState state) {
178+
public static void updateState(UUID uuid, Location location, UserStats stats) {
169179
lastLocations.put(uuid, location);
170-
lastStates.put(uuid, state);
180+
lastStats.put(uuid, stats);
171181
}
172182

173183
/**
174184
* Get the last known states of users.
175185
*
176186
* @return A map of user UUIDs to their last known UserState.
177187
*/
178-
public static HashMap<UUID, UserState> getLastStates() {
179-
return lastStates;
188+
public static HashMap<UUID, UserStats> getLastStats() {
189+
return lastStats;
180190
}
181191

182192
/**

src/main/java/tech/techstreet/border/lib/user/User.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,23 @@ public void setState(UserState state) {
7676

7777
if (state == UserState.PLAY) {
7878
Location location = ProgressHandler.getLastLocations().get(player.getUniqueId());
79+
UserStats stats = ProgressHandler.getLastStats().getOrDefault(player.getUniqueId(), new UserStats(UserState.PLAY, 20, 20, 20));
7980
if (location == null) location = new Location(Bukkit.getWorld("world"), 576.50, 67, -517.50);
8081

8182
player.teleport(location);
83+
player.setHealth(stats.health());
84+
player.setFoodLevel(stats.food());
85+
player.setSaturation(stats.saturation());
8286
player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1.0F, 1.0F);
8387
player.setScoreboard(BorderHoarderPlugin.getBorderHandler().getScoreboard());
8488
player.setGameMode(GameMode.SURVIVAL);
8589
}
8690

8791
if (state == UserState.LOBBY) {
8892
player.teleport(new Location(Bukkit.getWorld("world_spawn"), 0.5, 90, 0.5, 180, 0));
93+
player.setHealth(20);
94+
player.setFoodLevel(20);
95+
player.setSaturation(20);
8996
player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1.0F, 1.0F);
9097
player.setScoreboard(BorderHoarderPlugin.getBorderHandler().getScoreboard());
9198
player.setGameMode(GameMode.SURVIVAL);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package tech.techstreet.border.lib.user;
2+
3+
public record UserStats(UserState state, int food, float saturation, double health) {
4+
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: BorderHoarder
2-
version: '2.0.0'
2+
version: '2.0.1'
33
main: tech.techstreet.border.BorderHoarderPlugin
44
api-version: '1.21'

0 commit comments

Comments
 (0)