treasure chests
This commit is contained in:
parent
c3c82b4478
commit
04f6845e09
1 changed files with 31 additions and 17 deletions
|
@ -5,9 +5,11 @@ import static me.unurled.sacredrealms.sr.utils.Component.textComp;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import io.papermc.paper.event.packet.PlayerChunkLoadEvent;
|
import io.papermc.paper.event.packet.PlayerChunkLoadEvent;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
||||||
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
||||||
|
@ -27,7 +29,8 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class TreasureManager extends Manager {
|
public class TreasureManager extends Manager {
|
||||||
private final List<Treasure> treasures = new ArrayList<>();
|
private final Map<Integer, Treasure> treasures = new HashMap<>();
|
||||||
|
private final Map<Integer, Location> locations = new HashMap<>();
|
||||||
|
|
||||||
private int nextId = 0;
|
private int nextId = 0;
|
||||||
|
|
||||||
|
@ -35,12 +38,12 @@ public class TreasureManager extends Manager {
|
||||||
@Override
|
@Override
|
||||||
public void saveData() {
|
public void saveData() {
|
||||||
// save all treasures
|
// save all treasures
|
||||||
for (Treasure treasure : treasures) {
|
for (Treasure treasure : treasures.values()) {
|
||||||
// serialize treasure
|
// serialize treasure
|
||||||
DataHandler dh = Manager.getInstance(DataManager.class).getDataHandler();
|
DataHandler dh = Manager.getInstance(DataManager.class).getDataHandler();
|
||||||
Gson gson =
|
Gson gson =
|
||||||
new GsonBuilder().registerTypeAdapter(Treasure.class, new TreasureSerializer()).create();
|
new GsonBuilder().registerTypeAdapter(Treasure.class, new TreasureSerializer()).create();
|
||||||
dh.set("treasures." + treasures.indexOf(treasure), gson.toJson(treasure));
|
dh.set("treasures." + treasure.getId(), gson.toJson(treasure));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,28 +61,34 @@ public class TreasureManager extends Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTreasure(Treasure treasure) {
|
public void addTreasure(Treasure treasure) {
|
||||||
treasures.add(treasure);
|
treasures.put(treasure.getId(), treasure);
|
||||||
nextId = Math.max(nextId, treasure.getId() + 1);
|
nextId = Math.max(nextId, treasure.getId() + 1);
|
||||||
|
locations.put(treasure.getId(), treasure.getLocation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Treasure getTreasureById(Integer id) {
|
||||||
|
return treasures.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Treasure findByLocation(@NotNull Location loc) {
|
public Treasure findByLocation(@NotNull Location loc) {
|
||||||
for (Treasure treasure : treasures) {
|
for (Entry<Integer, Location> location : locations.entrySet()) {
|
||||||
if (treasure.getLocation().equals(loc)) {
|
if (location.getValue().equals(loc)) {
|
||||||
return treasure;
|
return getTreasureById(location.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Treasure findByNearbyPlayer(@NotNull Location loc) {
|
private Treasure findByNearbyPlayer(@NotNull Location loc) {
|
||||||
for (Treasure treasure : treasures) {
|
for (Entry<Integer, Location> location : locations.entrySet()) {
|
||||||
if (treasure.getLocation().getWorld() != loc.getWorld()) {
|
if (location.getValue().getWorld() != loc.getWorld()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (treasure.getLocation().distance(loc) < 5) {
|
if (location.getValue().distance(loc) < 5) {
|
||||||
return treasure;
|
return getTreasureById(location.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -87,7 +96,7 @@ public class TreasureManager extends Manager {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<Treasure> getTreasures() {
|
public List<Treasure> getTreasures() {
|
||||||
return treasures;
|
return treasures.values().stream().toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeTreasure(@NotNull Treasure treasure) {
|
public void removeTreasure(@NotNull Treasure treasure) {
|
||||||
|
@ -106,7 +115,7 @@ public class TreasureManager extends Manager {
|
||||||
player.sendBlockChange(treasure.getLocation(), Material.AIR.createBlockData());
|
player.sendBlockChange(treasure.getLocation(), Material.AIR.createBlockData());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
treasures.remove(treasure);
|
treasures.remove(treasure.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNextId() {
|
public int getNextId() {
|
||||||
|
@ -129,10 +138,15 @@ public class TreasureManager extends Manager {
|
||||||
private void onTreasureOpen(PlayerInteractEvent e, Block clickedBlock) {
|
private void onTreasureOpen(PlayerInteractEvent e, Block clickedBlock) {
|
||||||
// it is a chest, lets loop on all treasure and find if the clicked block is a treasure chest
|
// it is a chest, lets loop on all treasure and find if the clicked block is a treasure chest
|
||||||
|
|
||||||
for (Treasure treasure : treasures) {
|
for (Entry<Integer, Location> location : locations.entrySet()) {
|
||||||
if (treasure.getLocation().equals(clickedBlock.getLocation())) {
|
if (location.getValue().equals(clickedBlock.getLocation())) {
|
||||||
// it is a treasure chest, lets open it
|
// it is a treasure chest, lets open it
|
||||||
Player p = e.getPlayer();
|
Player p = e.getPlayer();
|
||||||
|
Treasure treasure = getTreasureById(location.getKey());
|
||||||
|
if (treasure == null) {
|
||||||
|
p.sendMessage(textComp("<red>An Error occurred contact an Admin."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!p.hasPermission(treasure.getPermission())) {
|
if (!p.hasPermission(treasure.getPermission())) {
|
||||||
p.sendMessage("You don't have permission to open this chest");
|
p.sendMessage("You don't have permission to open this chest");
|
||||||
p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 1f, 0.5f);
|
p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 1f, 0.5f);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue