Skip to content

Commit

Permalink
Adds a course GUI with warping. Use warped pressure plate to set spot
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 6, 2021
1 parent 30bb96b commit b69dbb4
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>0.0.2</build.version>
<build.version>0.1.0</build.version>
</properties>

<!-- Profiles will allow to automatically change build version. -->
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/world/bentobox/parkour/Parkour.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.parkour.commands.CoursesCommand;
import world.bentobox.parkour.commands.TopCommand;
import world.bentobox.parkour.generators.ChunkGeneratorWorld;
import world.bentobox.parkour.listeners.MakeCourseListener;
import world.bentobox.parkour.gui.RankingsUI;
import world.bentobox.parkour.listeners.CourseRunnerListener;
import world.bentobox.parkour.listeners.MakeCourseListener;

/**
* Main Parkour class
Expand Down Expand Up @@ -53,6 +55,7 @@ public void setup()
{
super.setup();
new TopCommand(getAddon(), this);
new CoursesCommand(getAddon(), this);
}
};

Expand Down
71 changes: 61 additions & 10 deletions src/main/java/world/bentobox/parkour/ParkourManager.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package world.bentobox.parkour;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.bukkit.Location;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.parkour.objects.HighScores;
import world.bentobox.parkour.objects.ParkourData;

public class ParkourManager {

// Database handler for level data
private final Database<HighScores> handler;
// A cache of high scores. Key is island UUID
private final Map<String, HighScores> cache;
private final Database<ParkourData> handler;
/**
* A cache of high scores. Key is island UUID
*/
private final Map<String, ParkourData> cache;

/**
* Handles storing a retrieval of score data for islands
Expand All @@ -29,23 +35,25 @@ public ParkourManager(Parkour addon) {
// Get the BentoBox database
// Set up the database handler to store and retrieve data
// Note that these are saved by the BentoBox database
handler = new Database<>(addon, HighScores.class);
handler = new Database<>(addon, ParkourData.class);
// Initialize the cache
cache = new ConcurrentHashMap<>();
// Load all
handler.loadObjects().forEach(hs -> cache.put(hs.getUniqueId(), hs));
}

private HighScores getIsland(Island island) {
private ParkourData getIsland(Island island) {
return cache.computeIfAbsent(island.getUniqueId(), k -> getFromDb(island));
}

private HighScores getFromDb(Island island) {
private ParkourData getFromDb(Island island) {
if (handler.objectExists(island.getUniqueId())) {
HighScores hs = handler.loadObject(island.getUniqueId());
ParkourData hs = handler.loadObject(island.getUniqueId());
if (hs != null) {
return hs;
}
}
return new HighScores(island.getUniqueId());
return new ParkourData(island.getUniqueId());
}

private void saveIsland(Island island) {
Expand All @@ -59,7 +67,7 @@ private void saveIsland(Island island) {
* @param time time in milliseconds
*/
public void addScore(Island island, User user, long time) {
HighScores h = getIsland(island);
ParkourData h = getIsland(island);
h.getRankings().put(user.getUniqueId(), time);
h.setRunCount(h.getRunCount() + 1);
// Save every time right now
Expand Down Expand Up @@ -114,7 +122,50 @@ public int getRank(Island island, UUID uuid) {
return (int) stream.takeWhile(x -> !x.getKey().equals(uuid)).map(Entry::getKey).count() + 1;
}

/**
* Get the time previously take by this player
* @param island island
* @param uniqueId player's UUID
* @return time or 0L if never down
*/
public long getTime(Island island, UUID uniqueId) {
return getIsland(island).getRankings().getOrDefault(uniqueId, 0L);
}

/**
* @return the HighScores
*/
public Collection<ParkourData> getParkourData() {
return cache.values();
}

public Optional<Location> getStart(Island island) {
return Optional.ofNullable(getIsland(island).getStart());
}

public Optional<Location> getEnd(Island island) {
return Optional.ofNullable(getIsland(island).getEnd());
}

public Optional<Location> getWarpSpot(Island island) {
return Optional.ofNullable(getIsland(island).getWarpSpot());
}

public void setStart(Island island, Location location) {
getIsland(island).setStart(location);
// Save every time right now
saveIsland(island);
}

public void setEnd(Island island, Location location) {
getIsland(island).setEnd(location);
// Save every time right now
saveIsland(island);
}

public void setWarpSpot(Island island, Location location) {
getIsland(island).setWarpSpot(location);
// Save every time right now
saveIsland(island);
}
}
41 changes: 41 additions & 0 deletions src/main/java/world/bentobox/parkour/commands/CoursesCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package world.bentobox.parkour.commands;

import java.util.List;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.builders.TabbedPanelBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.parkour.Parkour;
import world.bentobox.parkour.gui.CoursesTab;

public class CoursesCommand extends CompositeCommand {

public CoursesCommand(Parkour addon, CompositeCommand parent) {
super(parent, "courses", "tracks");
}

@Override
public void setup() {
this.setPermission("parkour.courses");
setOnlyPlayer(true);
setDescription("parkour.commands.parkour.courses.description");
}

@Override
public boolean canExecute(User user, String label, List<String> args) {
return true;
}

@Override
public boolean execute(User user, String label, List<String> args) {
new TabbedPanelBuilder()
.user(user)
.world(getWorld())
.tab(1, new CoursesTab(getAddon(), user))
.startingSlot(1)
.size(54)
.build().openPanel();
return true;
}

}
142 changes: 142 additions & 0 deletions src/main/java/world/bentobox/parkour/gui/CoursesTab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package world.bentobox.parkour.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.Tab;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.parkour.Parkour;
import world.bentobox.parkour.objects.ParkourData;

/**
* Implements a {@link Tab} that shows course rankings
* @author tastybento
* @since 1.0.0
*
*/
public class CoursesTab implements Tab {

private final Parkour addon;
private final User user;

/**
* Show a tab of settings
* @param user - user who is viewing the tab
* @param island - the island
* @param type - flag type
*/
public CoursesTab(Parkour addon, User user) {
super();
this.addon = addon;
this.user = user;
}

/**
* Get the icon for this tab
* @return panel item
*/
@Override
public PanelItem getIcon() {
PanelItemBuilder pib = new PanelItemBuilder();
// Set the icon
pib.icon(Material.HEAVY_WEIGHTED_PRESSURE_PLATE);
pib.name(getName());
pib.description(user.getTranslation("parkour.courses.description"));
return pib.build();
}

/* (non-Javadoc)
* @see world.bentobox.bentobox.api.panels.Tab#getName()
*/
@Override
public String getName() {
return user.getTranslation("parkour.courses.gui-title");
}

/**
* Get all the flags as panel items
* @return list of all the panel items for this flag type
*/
@Override
@NonNull
public List<@Nullable PanelItem> getPanelItems() {
// Create the list
List<PanelItem> heads = new ArrayList<>();
// Sort the courses by runs
addon.getPm().getParkourData().stream()
.sorted()
.filter(hs -> Objects.nonNull(hs.getWarpSpot()))
.forEach(hs -> {
UUID owner = addon.getIslands().getIslandById(hs.getUniqueId()).map(Island::getOwner).orElse(null);
if (owner != null) {
heads.add(getHead(hs, owner));
}
});
return heads;
}

/**
* Get the head panel item
* @param pd - parkour data
* @param playerUUID - the UUID of the owner
* @return PanelItem
*/
private PanelItem getHead(ParkourData pd, UUID playerUUID) {
final String name = addon.getPlayers().getName(playerUUID);
List<String> description = new ArrayList<>();
if (pd.getRunCount() > 0) {
description.add(user.getTranslation("parkour.courses.head-description", "[name]", name, "[runs]", String.valueOf(pd.getRunCount())));
}
if (addon.getIslands().inTeam(addon.getOverWorld(), playerUUID)) {
List<String> memberList = new ArrayList<>();
for (UUID members : addon.getIslands().getMembers(addon.getOverWorld(), playerUUID)) {
memberList.add(ChatColor.AQUA + addon.getPlayers().getName(members));
}
description.addAll(memberList);
}

PanelItemBuilder builder = new PanelItemBuilder()
.icon(name)
.name(name)
.clickHandler((panel, user, clickType, slot) -> {
user.sendMessage("parkour.warp.warping");
// Teleport user
Util.teleportAsync(user.getPlayer(), pd.getWarpSpot().clone().add(new Vector(0.5, 1, 0.5)), TeleportCause.COMMAND);
return true;
})
.description(description);
return builder.build();
}

@Override
public Map<Integer, PanelItem> getTabIcons() {
Map<Integer, PanelItem> icons = new HashMap<>();

return icons;
}

/* (non-Javadoc)
* @see world.bentobox.bentobox.api.panels.Tab#getPermission()
*/
@Override
public String getPermission() {
// All of these tabs can be seen by anyone
return "";
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package world.bentobox.parkour;
package world.bentobox.parkour.gui;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
Expand All @@ -11,6 +11,7 @@
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.parkour.Parkour;
import world.bentobox.parkour.listeners.AbstractListener;

/**
Expand Down Expand Up @@ -107,4 +108,5 @@ private PanelItem getHead(int rank, long time, UUID playerUUID, User asker) {
.description(description);
return builder.build();
}

}
4 changes: 4 additions & 0 deletions src/main/java/world/bentobox/parkour/gui/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains GUIs
*/
package world.bentobox.parkour.gui;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.event.Listener;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.parkour.Parkour;

public abstract class AbstractListener implements Listener {
Expand All @@ -17,8 +16,6 @@ public abstract class AbstractListener implements Listener {
static {
DF2.setRoundingMode(RoundingMode.UP);
}
protected static final String START = "Parkour_Start";
protected static final String END = "Parkour_End";
protected Parkour addon;
/**
* @param addon Parkour addon
Expand All @@ -28,9 +25,8 @@ public AbstractListener(Parkour addon) {

}

protected boolean isLocEquals(Location l1, String l2) {
Location l3 = Util.getLocationString(l2);
return l1.getWorld().equals(l3.getWorld()) && l1.getBlockX() == l3.getBlockX() && l1.getBlockY() == l3.getBlockY() && l1.getBlockZ() == l3.getBlockZ();
protected boolean isLocEquals(Location l1, Location l2) {
return l1.getWorld().equals(l2.getWorld()) && l1.getBlockX() == l2.getBlockX() && l1.getBlockY() == l2.getBlockY() && l1.getBlockZ() == l2.getBlockZ();
}

public static String getDuration(User user, long time) {
Expand Down

0 comments on commit b69dbb4

Please sign in to comment.