Skip to content

Commit

Permalink
addon ability set world slots manually - RTPMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperRonanCraft committed Jun 27, 2023
1 parent 7fd8b6d commit c8d076c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BetterRTPAddons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>me.SuperRonanCraft</groupId>
<artifactId>BetterRTPAddons</artifactId>
<packaging>jar</packaging>
<version>1.8.7</version>
<version>1.8.8</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class AddonRTPMenu implements Addon, Listener {

public static String name = "RTPMenu";
@Getter private final RTPMenu_Settings settings = new RTPMenu_Settings();
private final HashMap<Player, MenuData> playerData = new HashMap<>();
@Getter private HashMap<String, RTPMenuWorldInfo> worlds = new HashMap<>();

Expand All @@ -41,6 +42,7 @@ public void load() {
for (Player p : playerData.keySet())
p.closeInventory();
playerData.clear();
settings.load(name); //Load settings
ConfigurationSection config = getFile(Files.FILETYPE.CONFIG).getConfigurationSection("RTPMenu");
List<Map<?, ?>> map = config.getMapList("WorldList");
for (Map<?, ?> m : map) {
Expand All @@ -64,8 +66,13 @@ public void load() {
if (test.get("Lore").getClass() == ArrayList.class)
lore = new ArrayList<String>((ArrayList) test.get("Lore"));
}
int slot = 0;
if (test.get("Slot") != null) {
if (test.get("Slot").getClass() == Integer.class)
slot = (Integer) test.get("Slot");
}
try {
worlds.put(world, new RTPMenuWorldInfo(name, Material.valueOf(item.toUpperCase()), lore));
worlds.put(world, new RTPMenuWorldInfo(name, Material.valueOf(item.toUpperCase()), lore, slot));
} catch (IllegalArgumentException | NullPointerException e) {
Main.getInstance().getLogger().warning("The item " + item + " is an unknown item id! Set to a map!");
}
Expand Down Expand Up @@ -100,9 +107,8 @@ private void onClick(InventoryClickEvent e) {
private void onTeleport(RTP_CommandEvent e) {
if (e.getCmd() instanceof CmdTeleport && e.getSendi() instanceof Player) {
e.setCancelled(true);
int refresh = Files.FILETYPE.CONFIG.getInt(AddonRTPMenu.name + ".AutoRefresh");
Player player = (Player) e.getSendi();
new RTPMenu_Refresh(this, player, refresh);
new RTPMenu_Refresh(this, player, settings.getRefresh());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class RTPMenuWorldInfo {
public final String name;
public final Material item;
public final List<String> lore;
public final int slot;

RTPMenuWorldInfo(String name, Material item, List<String> lore) {
RTPMenuWorldInfo(String name, Material item, List<String> lore, int slot) {
this.name = name;
this.item = item;
this.lore = lore;
this.slot = slot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public static boolean createInv(AddonRTPMenu pl, Player p) {
CmdTeleport.teleport(p, "rtp", null, null);
return false;
}
int size = Math.floorDiv(actual_worlds.size(), 9) * 9;
if (size < actual_worlds.size()) size += 9;
Inventory inv = createInventory(color(p, Files.FILETYPE.CONFIG.getString(AddonRTPMenu.name + ".Title")), size);
int lines = pl.getSettings().getLines();
if (lines == 0)
lines = Math.floorDiv(actual_worlds.size(), 9);
if (lines < actual_worlds.size() / 9) lines++;
Inventory inv = createInventory(color(p, pl.getSettings().getTitle()), Math.min(lines * 9, 6 * 9));

HashMap<Integer, World> world_slots = centerWorlds(new ArrayList<>(actual_worlds));
HashMap<Integer, World> world_slots = centerWorlds(pl, new ArrayList<>(actual_worlds));

for (Map.Entry<Integer, World> world : world_slots.entrySet()) {
String worldName = world.getValue().getName();
RTPMenuWorldInfo worldInfo = pl.getWorlds().getOrDefault(worldName, new RTPMenuWorldInfo(worldName, Material.MAP, null));
RTPMenuWorldInfo worldInfo = pl.getWorlds().getOrDefault(worldName, new RTPMenuWorldInfo(worldName, Material.MAP, null, 0));
int slot = world.getKey();
ItemStack item = new ItemStack(worldInfo.item, 1);
ItemMeta meta = item.getItemMeta();
Expand All @@ -61,20 +63,26 @@ public static boolean createInv(AddonRTPMenu pl, Player p) {
return true;
}

private static HashMap<Integer, World> centerWorlds(List<World> actual_worlds) {
private static HashMap<Integer, World> centerWorlds(AddonRTPMenu pl, List<World> actual_worlds) {
HashMap<Integer, World> map = new HashMap<>();
while(actual_worlds.size() >= 9) {
for (int i = 0; i < 9; i++) {
map.put(map.size(), actual_worlds.get(0));
actual_worlds.remove(0);
if (actual_worlds.size() >= 9) {
for (int i = 0; i < actual_worlds.size(); i++) {
map.put(map.size(), actual_worlds.get(i));
//actual_worlds.remove(0);
}
return map;
}
int slot = map.size();
//BetterRTP.getInstance().getLogger().log(Level.INFO, "- " + actual_worlds.size());
for (int i = 0; i < actual_worlds.size(); i++) {
int offset = getSlotOffset(actual_worlds.size(), i);
//BetterRTP.getInstance().getLogger().log(Level.INFO, "- " + offset);
map.put(slot + offset + i, actual_worlds.get(i));
if (pl.getSettings().getAutoCenter()) {
for (int i = 0; i < actual_worlds.size(); i++) {
int offset = getSlotOffset(actual_worlds.size(), i);
map.put(offset + i, actual_worlds.get(i));
}
} else {
for (int i = 0; i < actual_worlds.size(); i++) {
RTPMenuWorldInfo info = pl.getWorlds().get(actual_worlds.get(i).getName());
if (info != null)
map.put(info.slot, actual_worlds.get(i));
}
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.SuperRonanCraft.BetterRTPAddons.addons.rtpmenu;

import lombok.Getter;
import me.SuperRonanCraft.BetterRTPAddons.util.Files;

public class RTPMenu_Settings {
@Getter private int refresh, lines;
@Getter private String title;
@Getter private Boolean autoCenter;

void load(String prefix) {
refresh = Files.FILETYPE.CONFIG.getInt(prefix + ".AutoRefresh");
lines = Files.FILETYPE.CONFIG.getInt(prefix + ".Lines");
title = Files.FILETYPE.CONFIG.getString(prefix + ".Title");
autoCenter = Files.FILETYPE.CONFIG.getBoolean(prefix + ".AutoCenter");
}
}
4 changes: 4 additions & 0 deletions BetterRTPAddons/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ RTPMenu:
Enabled: false
AutoRefresh: 0 #0 means menu will not refresh! (in ticks)
Title: "Choose World"
AutoCenter: true #If true, will ignore `Slot`, does not work if world list >= 9
Lines: 3
WorldList:
- world:
Name: '&7World'
Item: 'map'
Slot: 3
Lore:
- ''
- '&8Click to rtp in world!'
Expand All @@ -78,6 +81,7 @@ RTPMenu:
- world_nether:
Name: '&cNether'
Item: 'netherrack'
Slot: 5
Lore:
- ''
- '&8Click to rtp in world!'
Expand Down

0 comments on commit c8d076c

Please sign in to comment.