parent
858c366b36
commit
177a61b041
15 changed files with 479 additions and 205 deletions
@ -1,116 +0,0 @@ |
|||||||
package xyz.soukup.ecoCraftCore.commands; |
|
||||||
|
|
||||||
import com.mojang.brigadier.arguments.StringArgumentType; |
|
||||||
import dev.triumphteam.gui.builder.item.ItemBuilder; |
|
||||||
import dev.triumphteam.gui.guis.GuiItem; |
|
||||||
import io.papermc.paper.command.brigadier.CommandSourceStack; |
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
|
||||||
import io.papermc.paper.command.brigadier.Commands; |
|
||||||
import com.mojang.brigadier.context.CommandContext; |
|
||||||
import xyz.soukup.ecoCraftCore.utilities.Messages; |
|
||||||
import net.kyori.adventure.text.Component; |
|
||||||
import org.bukkit.command.CommandSender; |
|
||||||
import dev.triumphteam.gui.guis.Gui; |
|
||||||
import org.bukkit.entity.Player; |
|
||||||
import org.bukkit.Material; |
|
||||||
|
|
||||||
import java.util.HashMap; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
public class GuiCommand { |
|
||||||
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() { |
|
||||||
return Commands.literal("gopen") |
|
||||||
.then(Commands.argument("id", StringArgumentType.string()) |
|
||||||
.executes(GuiCommand::openGui)) |
|
||||||
.executes(GuiCommand::getAll); |
|
||||||
} |
|
||||||
|
|
||||||
private static int openGui(CommandContext<CommandSourceStack> context){ |
|
||||||
CommandSender commandSender = context.getSource().getSender(); |
|
||||||
String guiId = context.getArgument("id", String.class); |
|
||||||
|
|
||||||
if (!(commandSender instanceof Player)){ |
|
||||||
Messages.send(commandSender, "generic.error.not-player"); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
Gui gui = getGui(guiId, false, commandSender); // Pokud najdeme gui vrátíme ho, pokud ne vrátíme default gui
|
|
||||||
gui.open((Player) commandSender); |
|
||||||
return 1; |
|
||||||
} |
|
||||||
|
|
||||||
private static int getAll(CommandContext<CommandSourceStack> context){ |
|
||||||
CommandSender commandSender = context.getSource().getSender(); |
|
||||||
|
|
||||||
if (!(commandSender instanceof Player)){ |
|
||||||
Messages.send(commandSender, "generic.error.not-player"); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
getGui("", true, commandSender); // Pokud najdeme gui vrátíme ho, pokud ne vrátíme default gui
|
|
||||||
return 1; |
|
||||||
} |
|
||||||
|
|
||||||
private static Gui getGui(String guiId, Boolean getAll, CommandSender commandSender){ |
|
||||||
// Mapa gui objektů
|
|
||||||
Map<String, Gui> guiMap = new HashMap<>(); |
|
||||||
|
|
||||||
// Default gui
|
|
||||||
Gui gui1 = Gui.gui() // U custom gui dávat vždycky "GUI -" -> ../events/InventoryActionCancel
|
|
||||||
.title(Component.text("GUI - Default")) |
|
||||||
.rows(6) |
|
||||||
.create(); |
|
||||||
|
|
||||||
// Shop Admin gui
|
|
||||||
Gui shopAdminGui = Gui.gui() |
|
||||||
.title(Messages.get("gui.shopadmin.title")) |
|
||||||
.rows(3) |
|
||||||
.create(); |
|
||||||
shopAdminGui.getFiller().fill(ItemBuilder.from(Material.BLACK_STAINED_GLASS_PANE) |
|
||||||
.name(Messages.get("gui.shopadmin.items.background")).asGuiItem()); |
|
||||||
GuiItem deleteShop = ItemBuilder.from(Material.BARRIER).name(Messages.get("gui.shopadmin.items.deleteshop")) |
|
||||||
.asGuiItem(event -> { |
|
||||||
// Zde delete shop
|
|
||||||
}); |
|
||||||
GuiItem changeSellPrice = ItemBuilder.from(Material.OAK_SIGN).name(Messages.get("gui.shopadmin.items.changesellprice")) |
|
||||||
.asGuiItem(event -> { |
|
||||||
// Zde change sell price
|
|
||||||
}); |
|
||||||
GuiItem changeBuyPrice = ItemBuilder.from(Material.DARK_OAK_SIGN).name(Messages.get("gui.shopadmin.items.changebuyprice")) |
|
||||||
.asGuiItem(event -> { |
|
||||||
// Zde change buy price
|
|
||||||
}); |
|
||||||
GuiItem changeAmounts = ItemBuilder.from(Material.GOLD_BLOCK).name(Messages.get("gui.shopadmin.items.changeamounts")) |
|
||||||
.asGuiItem(event -> { |
|
||||||
// Zde change amounts
|
|
||||||
}); |
|
||||||
|
|
||||||
shopAdminGui.setItem(22, deleteShop); |
|
||||||
shopAdminGui.setItem(15, changeBuyPrice); |
|
||||||
shopAdminGui.setItem(11, changeSellPrice); |
|
||||||
shopAdminGui.setItem(13, changeAmounts); |
|
||||||
|
|
||||||
guiMap.put("default", gui1); |
|
||||||
guiMap.put("shop-admin", shopAdminGui); |
|
||||||
|
|
||||||
if (getAll){ |
|
||||||
printAll(guiMap, commandSender); |
|
||||||
} |
|
||||||
|
|
||||||
return guiMap.getOrDefault(guiId, gui1); |
|
||||||
} |
|
||||||
|
|
||||||
private static void printAll(Map<String, Gui> guiMap, CommandSender commandSender){ |
|
||||||
HashMap<String, String> stringGuis = new HashMap<>(); |
|
||||||
int index = 1; |
|
||||||
|
|
||||||
for (Map.Entry<String, Gui> entry : guiMap.entrySet()) { |
|
||||||
String guiName = entry.getKey(); |
|
||||||
stringGuis.put(String.valueOf(index), guiName); |
|
||||||
index++; // Každý číslo je placeholder v messages, pokud se změní počet gui musí se editnout i messages.yml
|
|
||||||
} |
|
||||||
|
|
||||||
Messages.send(commandSender, "gui.all", stringGuis); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@ -0,0 +1,4 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.commands; |
||||||
|
|
||||||
|
public class IslandCommand { |
||||||
|
} |
||||||
@ -1,17 +0,0 @@ |
|||||||
package xyz.soukup.ecoCraftCore.events; |
|
||||||
|
|
||||||
import org.bukkit.event.EventHandler; |
|
||||||
import org.bukkit.event.Listener; |
|
||||||
import org.bukkit.event.inventory.ClickType; |
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent; |
|
||||||
|
|
||||||
public class InventoryActionCancel implements Listener { |
|
||||||
@EventHandler |
|
||||||
public void onInventoryClick(InventoryClickEvent event) { |
|
||||||
if (event.getView().getTitle().contains("GUI -")) { |
|
||||||
if (!ClickType.valueOf(event.getClick().name()).toString().isEmpty()) { |
|
||||||
event.setCancelled(true); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,98 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.islands; |
||||||
|
import com.infernalsuite.asp.api.exceptions.UnknownWorldException; |
||||||
|
import com.infernalsuite.asp.api.loaders.SlimeLoader; |
||||||
|
import com.j256.ormlite.stmt.DeleteBuilder; |
||||||
|
import com.j256.ormlite.stmt.Where; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Island; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.DaoRegistry; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.sql.SQLException; |
||||||
|
import java.util.List; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
public class IslandLoader implements SlimeLoader { |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public byte[] readWorld(String worldName) throws UnknownWorldException, IOException { |
||||||
|
|
||||||
|
Island island; |
||||||
|
|
||||||
|
try { |
||||||
|
island = DaoRegistry.getIslandDao().queryBuilder().where().eq("uuid", worldName).queryForFirst(); |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
|
||||||
|
if (island == null){ |
||||||
|
throw new UnknownWorldException(worldName); |
||||||
|
} |
||||||
|
|
||||||
|
return island.getData(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean worldExists(String worldName) throws IOException { |
||||||
|
try { |
||||||
|
long count = DaoRegistry.getIslandDao().queryBuilder() |
||||||
|
.setCountOf(true) |
||||||
|
.where() |
||||||
|
.eq("uuid", worldName) |
||||||
|
.countOf(); |
||||||
|
|
||||||
|
return count > 0; |
||||||
|
|
||||||
|
} catch (NumberFormatException | SQLException e) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void saveWorld(String worldName, byte[] serializedWorld) throws IOException { |
||||||
|
try { |
||||||
|
Island island = DaoRegistry.getIslandDao().queryBuilder().where().eq("uuid", worldName).queryForFirst(); |
||||||
|
|
||||||
|
if (island != null) { |
||||||
|
island.setData(serializedWorld); |
||||||
|
island.save(); |
||||||
|
} else { |
||||||
|
throw new IOException("Island " + worldName + " not found in DB."); |
||||||
|
} |
||||||
|
} catch (NumberFormatException | SQLException e) { |
||||||
|
throw new IOException("Failed to save island : " + worldName, e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void deleteWorld(String worldName) throws IOException { |
||||||
|
try { |
||||||
|
DeleteBuilder<Island, Integer> deleteBuilder = DaoRegistry.getIslandDao().deleteBuilder(); |
||||||
|
deleteBuilder.where().eq("uuid", worldName); |
||||||
|
deleteBuilder.delete(); |
||||||
|
|
||||||
|
} catch (NumberFormatException | SQLException e) { |
||||||
|
throw new IOException("Failed to delete: " + worldName, e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<String> listWorlds() throws IOException { |
||||||
|
try { |
||||||
|
return DaoRegistry.getIslandDao().queryBuilder() |
||||||
|
.selectColumns("uuid") // Only fetch the ID column
|
||||||
|
.query() // This returns Island objects with ONLY the ID field populated
|
||||||
|
.stream() |
||||||
|
.map(Island::getUuid) |
||||||
|
.collect(Collectors.toList()); |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new IOException("Failed to list islands", e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,68 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.islands; |
||||||
|
|
||||||
|
import com.infernalsuite.asp.api.AdvancedSlimePaperAPI; |
||||||
|
import com.infernalsuite.asp.api.world.SlimeWorld; |
||||||
|
import com.infernalsuite.asp.api.world.properties.SlimeProperties; |
||||||
|
import com.infernalsuite.asp.api.world.properties.SlimePropertyMap; |
||||||
|
import org.bukkit.Bukkit; |
||||||
|
import org.bukkit.entity.Player; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Island; |
||||||
|
|
||||||
|
import java.util.Objects; |
||||||
|
import java.util.UUID; |
||||||
|
|
||||||
|
import static xyz.soukup.ecoCraftCore.EcoCraftCore.plugin; |
||||||
|
|
||||||
|
public class IslandManager { |
||||||
|
private final AdvancedSlimePaperAPI asp = AdvancedSlimePaperAPI.instance(); |
||||||
|
private final IslandLoader loader = new IslandLoader(); |
||||||
|
|
||||||
|
// 1. Create a brand new island
|
||||||
|
public void createIsland(String name, String displayName, String descritpion, String owner, String ownerType) { |
||||||
|
String uuid = UUID.randomUUID().toString(); |
||||||
|
|
||||||
|
|
||||||
|
SlimePropertyMap props = new SlimePropertyMap(); |
||||||
|
props.setValue(SlimeProperties.ENVIRONMENT, "overworld"); |
||||||
|
props.setValue(SlimeProperties.WORLD_TYPE, "flat"); |
||||||
|
|
||||||
|
// Create empty world in ASWM
|
||||||
|
try { |
||||||
|
// Note: createEmptyWorld is fast, so we can run some parts sync if needed,
|
||||||
|
// but it's best to run the whole chain async.
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { |
||||||
|
try { |
||||||
|
// Create the database entry first so the loader has a row to update
|
||||||
|
|
||||||
|
|
||||||
|
SlimeWorld slimeWorld = asp.createEmptyWorld(uuid, false, props, loader); |
||||||
|
Island island = new Island(name, uuid, displayName, descritpion, owner, ownerType, null); |
||||||
|
island.save(); |
||||||
|
|
||||||
|
Bukkit.getScheduler().runTask(plugin, () -> { |
||||||
|
asp.loadWorld(slimeWorld, true); |
||||||
|
}); |
||||||
|
} catch (Exception e) { e.printStackTrace(); } |
||||||
|
}); |
||||||
|
} catch (Exception e) { e.printStackTrace(); } |
||||||
|
} |
||||||
|
|
||||||
|
// 2. Retrieve and Load existing island
|
||||||
|
public void loadIsland(Player player, String uuidStr) { |
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { |
||||||
|
try { |
||||||
|
// Check if world is already loaded in Bukkit
|
||||||
|
if (Bukkit.getWorld(uuidStr) != null) { |
||||||
|
Bukkit.getScheduler().runTask(plugin, () -> player.teleport(Bukkit.getWorld(uuidStr).getSpawnLocation())); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
SlimeWorld slimeWorld = asp.readWorld(loader, uuidStr, false, new SlimePropertyMap()); |
||||||
|
Bukkit.getScheduler().runTask(plugin, () -> { |
||||||
|
asp.loadWorld(slimeWorld, true); |
||||||
|
player.teleport(Bukkit.getWorld(uuidStr).getSpawnLocation()); |
||||||
|
}); |
||||||
|
} catch (Exception e) { e.printStackTrace(); } |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,116 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.objects; |
||||||
|
|
||||||
|
import com.j256.ormlite.field.DataType; |
||||||
|
import com.j256.ormlite.field.DatabaseField; |
||||||
|
import com.j256.ormlite.table.DatabaseTable; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.DaoRegistry; |
||||||
|
|
||||||
|
import java.sql.SQLException; |
||||||
|
|
||||||
|
@DatabaseTable(tableName = "islands") |
||||||
|
public class Island { |
||||||
|
@DatabaseField(generatedId = true) |
||||||
|
private int id; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false, unique = true) |
||||||
|
private String uuid; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false, unique = true) |
||||||
|
private String name; |
||||||
|
|
||||||
|
|
||||||
|
@DatabaseField(columnName = "display_name") |
||||||
|
private String displayName; |
||||||
|
|
||||||
|
@DatabaseField() |
||||||
|
private String descritpion; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false) |
||||||
|
private String owner; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false) |
||||||
|
private String ownerType; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false, dataType = DataType.BYTE_ARRAY, columnDefinition = "LONGBLOB") |
||||||
|
private byte[] data; |
||||||
|
|
||||||
|
@DatabaseField(defaultValue = "", columnName = "active_on") |
||||||
|
private String activeOn; |
||||||
|
|
||||||
|
public Island(){ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Island(String name, String uuid, String displayName, String descritpion, String owner, String ownerType, byte[] data) { |
||||||
|
this.name = name; |
||||||
|
this.uuid = uuid; |
||||||
|
this.displayName = displayName; |
||||||
|
this.descritpion = descritpion; |
||||||
|
this.owner = owner; |
||||||
|
this.ownerType = ownerType; |
||||||
|
this.data = data; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void setData(byte[] data) { |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
|
||||||
|
public int getId() { |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
public String getName() { |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDisplayName() { |
||||||
|
return displayName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDescritpion() { |
||||||
|
return descritpion; |
||||||
|
} |
||||||
|
|
||||||
|
public String getOwner() { |
||||||
|
return owner; |
||||||
|
} |
||||||
|
|
||||||
|
public String getOwnerType() { |
||||||
|
return ownerType; |
||||||
|
} |
||||||
|
|
||||||
|
public byte[] getData() { |
||||||
|
return data; |
||||||
|
} |
||||||
|
|
||||||
|
public String getActiveOn() { |
||||||
|
return activeOn; |
||||||
|
} |
||||||
|
|
||||||
|
public static Island findById(int id) { |
||||||
|
|
||||||
|
|
||||||
|
try { |
||||||
|
return DaoRegistry.getIslandDao().queryForId(id); |
||||||
|
} catch (SQLException e) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void save(){ |
||||||
|
try { |
||||||
|
DaoRegistry.getIslandDao().createOrUpdate(this); |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public String getUuid() { |
||||||
|
return uuid; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.utilities; |
||||||
|
|
||||||
|
import com.github.stefvanschie.inventoryframework.gui.GuiItem; |
||||||
|
import net.kyori.adventure.text.Component; |
||||||
|
import org.bukkit.Material; |
||||||
|
import org.bukkit.inventory.ItemStack; |
||||||
|
import org.bukkit.inventory.meta.ItemMeta; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Objects; |
||||||
|
|
||||||
|
public class GuiItemBuilder { |
||||||
|
private ItemStack itemStack; |
||||||
|
private ItemMeta itemMeta; |
||||||
|
|
||||||
|
public GuiItemBuilder(Material material){ |
||||||
|
this.itemStack = new ItemStack(material); |
||||||
|
this.itemMeta = this.itemStack.getItemMeta(); |
||||||
|
} |
||||||
|
|
||||||
|
public GuiItemBuilder setName(Component component){ |
||||||
|
this.itemMeta.displayName(component); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public GuiItemBuilder addLore(Component component){ |
||||||
|
List<Component> lore = itemMeta.lore(); |
||||||
|
Objects.requireNonNull(lore).add(component); |
||||||
|
itemMeta.lore(lore); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public GuiItem build(){ |
||||||
|
this.itemStack.setItemMeta(this.itemMeta); |
||||||
|
return new GuiItem(this.itemStack); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue