Skip to content

Commit 65dc5ef

Browse files
committed
Add max_health sharable
1 parent acc420a commit 65dc5ef

File tree

7 files changed

+105
-15
lines changed

7 files changed

+105
-15
lines changed

src/main/java/org/mvplugins/multiverse/inventories/share/Sharables.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,31 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
174174
}).serializer(new ProfileEntry(false, DataStrings.PLAYER_OFF_HAND_ITEM),
175175
new DefaultSerializer<>(ItemStack.class)).altName("shield").build();
176176

177+
/**
178+
* Sharing Max Health.
179+
*/
180+
public static final Sharable<Double> MAX_HEALTH = new Sharable.Builder<>("max_hit_points", Double.class,
181+
new SharableHandler<Double>() {
182+
@Override
183+
public void updateProfile(PlayerProfile profile, Player player) {
184+
profile.set(MAX_HEALTH, getMaxHealth(player));
185+
}
186+
187+
@Override
188+
public boolean updatePlayer(Player player, PlayerProfile profile) {
189+
Double value = profile.get(MAX_HEALTH);
190+
if (value == null) {
191+
Option.of(maxHealthAttr).map(player::getAttribute)
192+
.peek(attr -> attr.setBaseValue(attr.getDefaultValue()));
193+
return false;
194+
}
195+
Option.of(maxHealthAttr).map(player::getAttribute)
196+
.peek(attr -> attr.setBaseValue(value));
197+
return true;
198+
}
199+
}).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_MAX_HEALTH))
200+
.altName("maxhealth").altName("maxhp").altName("maxhitpoints").build();
201+
177202
/**
178203
* Sharing Health.
179204
*/
@@ -184,9 +209,7 @@ public void updateProfile(PlayerProfile profile, Player player) {
184209
double health = player.getHealth();
185210
// Player is dead, so health should be regained to full.
186211
if (health <= 0) {
187-
health = Option.of(maxHealthAttr).map(player::getAttribute)
188-
.map(AttributeInstance::getValue)
189-
.getOrElse(PlayerStats.HEALTH);
212+
health = getMaxHealth(player);
190213
}
191214
profile.set(HEALTH, health);
192215
}
@@ -199,20 +222,31 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
199222
return false;
200223
}
201224
try {
225+
double maxHealth = getMaxHealth(player);
226+
// This share may handled before MAX_HEALTH.
227+
// Thus this is needed to ensure there is no loss in health stored
228+
if (value > maxHealth) {
229+
Option.of(maxHealthAttr).map(player::getAttribute)
230+
.peek(attr -> attr.setBaseValue(maxHealth));
231+
}
202232
player.setHealth(value);
203233
} catch (IllegalArgumentException e) {
204234
Logging.fine("Invalid value '" + value + "': " + e.getMessage());
205-
Option.of(maxHealthAttr).map(player::getAttribute)
206-
.map(AttributeInstance::getValue)
207-
.peek(player::setHealth)
208-
.onEmpty(() -> player.setHealth(PlayerStats.HEALTH));
235+
player.setHealth(PlayerStats.HEALTH);
209236
return false;
210237
}
211238
return true;
212239
}
240+
213241
}).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_HEALTH))
214242
.altName("health").altName("hp").altName("hitpoints").build();
215243

244+
private static double getMaxHealth(Player player) {
245+
return Option.of(maxHealthAttr).map(player::getAttribute)
246+
.map(AttributeInstance::getValue)
247+
.getOrElse(PlayerStats.MAX_HEALTH);
248+
}
249+
216250
/**
217251
* Sharing Remaining Air.
218252
*/
@@ -661,21 +695,21 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
661695
* Grouping for player health related sharables.
662696
*/
663697
public static final SharableGroup ALL_HEALTH = new SharableGroup("health",
664-
fromSharables(HEALTH, REMAINING_AIR, MAXIMUM_AIR, FALL_DISTANCE, FIRE_TICKS));
698+
fromSharables(HEALTH, MAX_HEALTH, REMAINING_AIR, MAXIMUM_AIR, FALL_DISTANCE, FIRE_TICKS));
665699

666700
/**
667701
* Grouping for player stat related sharables not including inventory.
668702
*/
669703
public static final SharableGroup STATS = new SharableGroup("stats",
670-
fromSharables(HEALTH, FOOD_LEVEL, SATURATION, EXHAUSTION, EXPERIENCE, TOTAL_EXPERIENCE, LEVEL,
704+
fromSharables(HEALTH, MAX_HEALTH, FOOD_LEVEL, SATURATION, EXHAUSTION, EXPERIENCE, TOTAL_EXPERIENCE, LEVEL,
671705
REMAINING_AIR, MAXIMUM_AIR, FALL_DISTANCE, FIRE_TICKS, POTIONS));
672706

673707
/**
674708
* Grouping for ALL default sharables.
675709
* TODO: make this really mean all, including 3rd party.
676710
*/
677-
public static final SharableGroup ALL_DEFAULT = new SharableGroup("all", fromSharables(HEALTH, ECONOMY,
678-
FOOD_LEVEL, SATURATION, EXHAUSTION, EXPERIENCE, TOTAL_EXPERIENCE, LEVEL, INVENTORY, ARMOR, BED_SPAWN,
711+
public static final SharableGroup ALL_DEFAULT = new SharableGroup("all", fromSharables(HEALTH, MAX_HEALTH,
712+
ECONOMY, FOOD_LEVEL, SATURATION, EXHAUSTION, EXPERIENCE, TOTAL_EXPERIENCE, LEVEL, INVENTORY, ARMOR, BED_SPAWN,
679713
MAXIMUM_AIR, REMAINING_AIR, FALL_DISTANCE, FIRE_TICKS, POTIONS, LAST_LOCATION, ENDER_CHEST, OFF_HAND),
680714
"*", "everything");
681715

src/main/java/org/mvplugins/multiverse/inventories/util/DataStrings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public final class DataStrings {
5757
* Player profile type identifier.
5858
*/
5959
public static final String PLAYER_PROFILE_TYPE = "profileType";
60+
/**
61+
* Player max health identifier.
62+
*/
63+
public static final String PLAYER_MAX_HEALTH = "mhp";
6064
/**
6165
* Player health identifier.
6266
*/

src/main/java/org/mvplugins/multiverse/inventories/util/PlayerStats.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class PlayerStats {
1717
* Number of slots in an ender chest.
1818
*/
1919
public static final int ENDER_CHEST_SIZE = 27;
20+
/**
21+
* Default max health value.
22+
*/
23+
public static final double MAX_HEALTH = 20;
2024
/**
2125
* Default health value.
2226
*/

src/test/java/org/mvplugins/multiverse/inventories/gameplay/GameModeChangeTest.kt renamed to src/test/java/org/mvplugins/multiverse/inventories/handleshare/GameModeChangeTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mvplugins.multiverse.inventories.gameplay
1+
package org.mvplugins.multiverse.inventories.handleshare
22

33
import org.mvplugins.multiverse.inventories.TestWithMockBukkit
44

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.mvplugins.multiverse.inventories.handleshare
2+
3+
import org.mockbukkit.mockbukkit.entity.PlayerMock
4+
import org.mvplugins.multiverse.inventories.TestWithMockBukkit
5+
import org.mvplugins.multiverse.inventories.profile.ProfileDataSource
6+
import org.mvplugins.multiverse.inventories.profile.ProfileTypes
7+
import org.mvplugins.multiverse.inventories.profile.container.ContainerType
8+
import org.mvplugins.multiverse.inventories.share.Sharables
9+
import kotlin.test.BeforeTest
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
class ShareHandlingUpdaterTest : TestWithMockBukkit() {
14+
15+
private lateinit var profileDataSource: ProfileDataSource
16+
private lateinit var player: PlayerMock
17+
18+
@BeforeTest
19+
fun setUp() {
20+
player = server.addPlayer("benthecat10")
21+
profileDataSource = serviceLocator.getService(ProfileDataSource::class.java).takeIf { it != null } ?: run {
22+
throw IllegalStateException("ProfileDataSource is not available as a service")
23+
}
24+
}
25+
26+
@Test
27+
fun `Test updating profile`() {
28+
player.health = 4.4
29+
player.maxHealth = 15.1
30+
31+
val playerProfile = profileDataSource.getPlayerData(ContainerType.WORLD, "world", ProfileTypes.SURVIVAL, player.uniqueId)
32+
ShareHandlingUpdater.updateProfile(multiverseInventories, player, PersistingProfile(Sharables.allOf(), playerProfile))
33+
34+
assertEquals(4.4, playerProfile.get(Sharables.HEALTH))
35+
assertEquals(15.1, playerProfile.get(Sharables.MAX_HEALTH))
36+
}
37+
38+
@Test
39+
fun `Test updating player`() {
40+
val playerProfile = profileDataSource.getPlayerData(ContainerType.WORLD, "world", ProfileTypes.SURVIVAL, player.uniqueId)
41+
playerProfile.set(Sharables.HEALTH, 4.4)
42+
playerProfile.set(Sharables.MAX_HEALTH, 15.1)
43+
44+
ShareHandlingUpdater.updatePlayer(multiverseInventories, player, PersistingProfile(Sharables.allOf(), playerProfile))
45+
46+
assertEquals(4.4, player.health)
47+
assertEquals(15.1, player.maxHealth)
48+
}
49+
}

src/test/java/org/mvplugins/multiverse/inventories/gameplay/WorldChangeTest.kt renamed to src/test/java/org/mvplugins/multiverse/inventories/handleshare/WorldChangeTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mvplugins.multiverse.inventories.gameplay
1+
package org.mvplugins.multiverse.inventories.handleshare
22

33
import com.dumptruckman.minecraft.util.Logging
44
import org.bukkit.Material

src/test/java/org/mvplugins/multiverse/inventories/gameplay/PlayerNameChangeTest.kt renamed to src/test/java/org/mvplugins/multiverse/inventories/profile/PlayerNameChangeTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package org.mvplugins.multiverse.inventories.gameplay
1+
package org.mvplugins.multiverse.inventories.profile
22

33
import org.bukkit.Material
44
import org.bukkit.inventory.ItemStack
55
import org.mockbukkit.mockbukkit.entity.PlayerMock
66
import org.mvplugins.multiverse.core.world.WorldManager
77
import org.mvplugins.multiverse.core.world.options.CreateWorldOptions
88
import org.mvplugins.multiverse.inventories.TestWithMockBukkit
9-
import org.mvplugins.multiverse.inventories.profile.ProfileDataSource
109
import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager
1110
import java.nio.file.Path
1211
import kotlin.test.BeforeTest

0 commit comments

Comments
 (0)