Skip to content

Commit

Permalink
Added "OnLeave.RetainResumableInventory" setting to restore course in…
Browse files Browse the repository at this point in the history
…ventory (untested)
  • Loading branch information
A5H73Y committed Jul 7, 2024
1 parent b40124d commit 72b486b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Build

env:
artifact_name: 'Parkour-7.2.3'
artifact_name: 'Parkour-7.2.4'
release_type: '-RELEASE'

on: push
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ For the list of known supported plugins and tutorials on how to set them up, [cl
<dependency>
<groupId>com.github.A5H73Y</groupId>
<artifactId>Parkour</artifactId>
<version>7.2.3</version>
<version>7.2.4</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
Expand All @@ -47,5 +47,5 @@ repositories {
```

```
compile 'com.github.A5H73Y:Parkour:7.2.3'
compile 'com.github.A5H73Y:Parkour:7.2.4'
```
4 changes: 4 additions & 0 deletions docs/changelogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Changelogs

Please note that each version of Parkour is backwards compatible with the previous version and will automatically upgrade your config upon server start up. There will be no manual intervention, unless stated in breaking changes.

## 7.2.4

* Added "OnLeave.RetainResumableInventory" setting to restore course inventory

## 7.2.3

* Added "%COURSE_NAME%" internal placeholder
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.a5h73y</groupId>
<artifactId>Parkour</artifactId>
<version>7.2.3</version>
<version>7.2.4</version>
<packaging>jar</packaging>

<name>Parkour</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public DefaultConfig(File file) {
this.setDefault("OnLeave.DestroyCourseProgress", true);
this.setDefault("OnLeave.TeleportAway", true);
this.setDefault("OnLeave.GiveGainedItemsBack", false);
this.setDefault("OnLeave.RetainResumableInventory", false);

this.setDefault("OnRestart.FullPlayerRestart", false);
this.setDefault("OnRestart.RequireConfirmation", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.github.a5h73y.parkour.type.player;

import io.github.a5h73y.parkour.type.player.session.ParkourSession;
import java.io.File;

import de.leonhard.storage.Json;
import de.leonhard.storage.internal.FileType;
import io.github.a5h73y.parkour.Parkour;
import io.github.a5h73y.parkour.type.player.session.ParkourSession;
import io.github.a5h73y.parkour.utility.TranslationUtils;
import java.io.File;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -43,7 +42,7 @@ public class PlayerConfig extends Json {
public static final String SNAPSHOT_PREFIX = "Snapshot.";
public static final String SESSION_PREFIX = "Session.";
public static final String STATS_PREFIX = "Stats.";

public static final String RESUMABLE_PREFIX = "Resumable.";

public PlayerConfig(File playerFile) {
super(playerFile);
Expand Down Expand Up @@ -193,8 +192,7 @@ public void setLastRewardedTime(String courseName, long rewardTime) {
*/
public void setPlayerDataSnapshot(Player player) {
if (!hasPlayerDataSnapshot()) {
this.setSerializable(SNAPSHOT_PREFIX + INVENTORY, player.getInventory().getContents());
this.setSerializable(SNAPSHOT_PREFIX + ARMOR, player.getInventory().getArmorContents());
this.setPlayerInventoryContents(SNAPSHOT_PREFIX, player);
this.set(SNAPSHOT_PREFIX + HEALTH, player.getHealth());
this.set(SNAPSHOT_PREFIX + HUNGER, player.getFoodLevel());
this.set(SNAPSHOT_PREFIX + XP_LEVEL, player.getLevel());
Expand All @@ -206,6 +204,31 @@ public void setPlayerDataSnapshot(Player player) {
}
}

public void setPlayerDataResumable(Player player) {
this.setPlayerInventoryContents(RESUMABLE_PREFIX, player);
}

public boolean hasResumableInventory() {
return this.contains(RESUMABLE_PREFIX + INVENTORY);
}

public ItemStack[] getResumableInventory() {
return this.getSerializable(RESUMABLE_PREFIX + INVENTORY, ItemStack[].class);
}

public ItemStack[] getResumableArmor() {
return this.getSerializable(RESUMABLE_PREFIX + ARMOR, ItemStack[].class);
}

public void resetResumableInventory() {
this.remove(RESUMABLE_PREFIX);
}

private void setPlayerInventoryContents(String configPrefix, Player player) {
this.setSerializable(configPrefix + INVENTORY, player.getInventory().getContents());
this.setSerializable(configPrefix + ARMOR, player.getInventory().getArmorContents());
}

/**
* Set the Snapshot data for the Player.
* This must not be overridden as the Player can join multiple courses without this data being touched.
Expand All @@ -221,6 +244,10 @@ public ItemStack[] getSnapshotStartParkourInventory() {
return this.getSerializable(SNAPSHOT_PREFIX + START_PARKOUR_INVENTORY, ItemStack[].class);
}

public boolean hasSnapshotStartParkourInventory() {
return this.contains(SNAPSHOT_PREFIX + START_PARKOUR_INVENTORY);
}

public boolean hasPlayerDataSnapshot() {
return this.contains(SNAPSHOT_PREFIX);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ public void joinCourse(Player player, Course course, boolean silent) {
playerConfig.setSnapshotStartParkourInventory(player);
}

if (playerConfig.hasResumableInventory()) {
PlayerUtils.setInventoryAndArmor(player, playerConfig.getResumableInventory(), playerConfig.getResumableArmor());
}

parkour.getSoundsManager().playSound(player, SoundType.JOIN_COURSE);
parkour.getConfigManager().getCourseConfig(course.getName()).incrementViews();
Bukkit.getServer().getPluginManager().callEvent(new ParkourJoinEvent(player, course.getName(), silent));
Expand Down Expand Up @@ -250,15 +254,20 @@ public void leaveCourse(Player player, boolean silent) {
}

teardownParkourMode(player);
PlayerConfig playerConfig = parkour.getConfigManager().getPlayerConfig(player);

if (session.isMarkedForDeletion()) {
parkour.getParkourSessionManager().deleteParkourSession(player, session.getCourseName());
parkour.getParkourSessionManager().removePlayer(player);
playerConfig.resetResumableInventory();

} else {
parkour.getParkourSessionManager().saveParkourSession(player, true);
if (parkour.getParkourConfig().getBoolean("OnLeave.RetainResumableInventory")) {
playerConfig.setPlayerDataResumable(player);
}
}

PlayerConfig playerConfig = parkour.getConfigManager().getPlayerConfig(player);

if (!silent) {
parkour.getSoundsManager().playSound(player, SoundType.COURSE_FAILED);
parkour.getBountifulApi().sendSubTitle(player,
Expand Down Expand Up @@ -844,12 +853,8 @@ public void restoreInventoryArmor(Player player, PlayerConfig playerConfig) {
TranslationUtils.sendMessage(player, "No saved inventory to load.");
return;
}

player.getInventory().clear();
player.getInventory().setContents(inventoryContents);

ItemStack[] armorContents = playerConfig.getSnapshotArmor();
player.getInventory().setArmorContents(armorContents);
PlayerUtils.setInventoryAndArmor(player, inventoryContents, armorContents);
} else {
// TODO attempt to remove the Parkour Tools
}
Expand Down Expand Up @@ -1299,7 +1304,7 @@ private void restorePlayerData(Player player, PlayerConfig playerConfig, boolean

private List<ItemStack> getItemsToRestore(Player player, PlayerConfig playerConfig, boolean finishedCourse) {
List<ItemStack> itemsToGiveBack = new ArrayList<>();
if (playerConfig.getSnapshotStartParkourInventory() != null
if (playerConfig.hasSnapshotStartParkourInventory()
&& ((parkour.getParkourConfig().getBoolean("OnFinish.GiveGainedItemsBack") && finishedCourse)
|| (parkour.getParkourConfig().getBoolean("OnLeave.GiveGainedItemsBack") && !finishedCourse))) {
List<String> parkourTools = getEachParkourToolMaterial();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.Server;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -261,9 +262,14 @@ public static void clearInventoryArmor(Player player) {
player.getInventory().setChestplate(null);
player.getInventory().setLeggings(null);
player.getInventory().setBoots(null);

player.updateInventory();
}

public static void setInventoryAndArmor(Player player, ItemStack[] inventory, ItemStack[] armor) {
player.getInventory().clear();
player.getInventory().setContents(inventory);
player.getInventory().setArmorContents(armor);
}

private PlayerUtils() {}
}

0 comments on commit 72b486b

Please sign in to comment.