This commit is contained in:
2vb 2024-07-04 22:51:31 -07:00
parent ab94e7f557
commit 9938367f72
8 changed files with 194 additions and 4 deletions

11
pom.xml
View File

@ -67,11 +67,16 @@
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
<relocations> <relocations>
<relocation> <relocation>
<pattern>xyz.twovb.toolbox</pattern> <pattern>xyz.twovb.toolbox</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}.toolbox</shadedPattern> <shadedPattern>${project.groupId}.${project.artifactId}.toolbox</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>com.github.stefvanschie.inventoryframework</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}.inventoryframework</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</plugin> </plugin>
@ -159,7 +164,11 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.16.1</version> <version>2.16.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>IF</artifactId>
<version>0.10.15</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -40,7 +40,6 @@ public final class SGM extends JavaPlugin {
@Getter @Getter
private LevelManager levelManager; private LevelManager levelManager;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
@ -55,12 +54,18 @@ public final class SGM extends JavaPlugin {
BuildInfo.load(Objects.requireNonNull(this.getTextResource("build-info.yml"))); BuildInfo.load(Objects.requireNonNull(this.getTextResource("build-info.yml")));
} catch (IOException | InvalidConfigurationException ignored) { } catch (IOException | InvalidConfigurationException ignored) {
} }
loadGuis();
registerCommands(); registerCommands();
registerGames(); registerGames();
registerPlaceholders(); registerPlaceholders();
levelManager.loadLevels(); levelManager.loadLevels();
} }
private void loadGuis() {
saveResource("guis/initgame.xml", false);
saveResource("guis/mapgui.xml", false);
}
private void registerPlaceholders() { private void registerPlaceholders() {
PlaceholderManager pm = new PlaceholderManager(); PlaceholderManager pm = new PlaceholderManager();
pm.registerPlaceholder("%commit.id%", BuildInfo.getString("commit.id")); pm.registerPlaceholder("%commit.id%", BuildInfo.getString("commit.id"));

View File

@ -4,6 +4,7 @@ package xyz.twovb.sgm.commands.impl;
* Created by 2vb - 2/7/2024 * Created by 2vb - 2/7/2024
*/ */
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import dev.rollczi.litecommands.annotations.argument.Arg; import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.context.Context;
@ -11,13 +12,16 @@ import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission; import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import xyz.twovb.sgm.SGM; import xyz.twovb.sgm.SGM;
import xyz.twovb.sgm.games.Minigame; import xyz.twovb.sgm.games.Minigame;
import xyz.twovb.sgm.games.impl.TestGame; import xyz.twovb.sgm.games.impl.TestGame;
import xyz.twovb.sgm.games.impl.capturethebrick.CTB; import xyz.twovb.sgm.games.impl.capturethebrick.CTB;
import xyz.twovb.sgm.guis.InitGameGui;
import xyz.twovb.toolbox.api.CustomPlayer; import xyz.twovb.toolbox.api.CustomPlayer;
import xyz.twovb.toolbox.utils.ChatUtils; import xyz.twovb.toolbox.utils.ChatUtils;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Command(name = "game") @Command(name = "game")
@ -40,9 +44,10 @@ public class GameCommand {
@Execute(name = "init") @Execute(name = "init")
@Permission("sgm.games.init") @Permission("sgm.games.init")
void init(@Context Player player, @Arg("name") String name) { void init(@Context Player player, @Arg("name") Optional<String> map) {
CustomPlayer cPlayer = new CustomPlayer(player); CustomPlayer cPlayer = new CustomPlayer(player);
SGM.getInstance().getGameManager().initGame(new CTB(), player, name); new InitGameGui().getGui().show(player);
// SGM.getInstance().getGameManager().initGame(new CTB(), player, name);
} }
@Execute(name = "start") @Execute(name = "start")
@ -53,6 +58,11 @@ public class GameCommand {
game.start(); game.start();
} }
// @Execute(name = "join")
// void join(@Context Player player) {
//
// }
@Execute(name = "join") @Execute(name = "join")
void join(@Context Player player, @Arg("id") String id) { void join(@Context Player player, @Arg("id") String id) {
CustomPlayer cPlayer = new CustomPlayer(player); CustomPlayer cPlayer = new CustomPlayer(player);

View File

@ -0,0 +1,55 @@
package xyz.twovb.sgm.guis;
/*
* Created by 2vb - 4/7/2024
*/
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import xyz.twovb.sgm.SGM;
import xyz.twovb.sgm.games.impl.capturethebrick.CTB;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.logging.Level;
public class InitGameGui {
@Getter
private final ChestGui gui;
public InitGameGui() {
ChestGui loadedGui = null;
try {
File guiFile = new File(SGM.getInstance().getDataFolder(), "guis/initgame.xml");
if (guiFile.exists()) {
byte[] fileContent = Files.readAllBytes(guiFile.toPath());
InputStream inputStream = new ByteArrayInputStream(fileContent);
loadedGui = ChestGui.load(this, inputStream);
} else {
SGM.getInstance().getLogger().log(Level.WARNING, "Could not find initgame.xml file.");
}
} catch (Exception e) {
SGM.getInstance().getLogger().log(Level.SEVERE, "Error loading initgame.xml", e);
}
gui = loadedGui;
}
public void globalClick(InventoryClickEvent event) {
event.setCancelled(true);
}
public void initGame(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
String itemName = event.getCurrentItem().getItemMeta().getDisplayName().replaceAll("\\W+", "");
player.closeInventory();
if (SGM.getInstance().getGameManager().getRegisteredGames().contains(itemName.toLowerCase())) {
new MapGui(itemName.toLowerCase()).getGui().show(player);
} else {
player.sendMessage("Please check xml");
}
}
}

View File

@ -0,0 +1,77 @@
package xyz.twovb.sgm.guis;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.twovb.sgm.SGM;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.logging.Level;
public class MapGui {
@Getter
private final ChestGui gui;
private final String game;
private OutlinePane mapPane;
public MapGui(String game) {
this.game = game;
ChestGui loadedGui = null;
try {
File guiFile = new File(SGM.getInstance().getDataFolder(), "guis/mapgui.xml");
if (guiFile.exists()) {
byte[] fileContent = Files.readAllBytes(guiFile.toPath());
InputStream inputStream = new ByteArrayInputStream(fileContent);
loadedGui = ChestGui.load(this, inputStream);
mapPane = new OutlinePane(1, 1, 7, 1); // Define your pane layout
loadedGui.addPane(mapPane);
addMaps();
} else {
SGM.getInstance().getLogger().log(Level.WARNING, "Could not find mapgui.xml file.");
}
} catch (Exception e) {
SGM.getInstance().getLogger().log(Level.SEVERE, "Error loading mapgui.xml", e);
}
gui = loadedGui;
}
public void globalClick(InventoryClickEvent event) {
event.setCancelled(true);
// event.getWhoClicked().sendMessage(this.game);
// gui.getInventory().clear();
}
public void addMaps() {
List<String> enabledMaps = SGM.getInstance().getLevelManager().enabledMaps.get(this.game);
if (enabledMaps != null) {
for (String mapName : enabledMaps) {
// Create button for each map
ItemStack mapItem = new ItemStack(Material.MAP);
ItemMeta mapMeta = mapItem.getItemMeta();
mapMeta.setDisplayName(mapName);
mapItem.setItemMeta(mapMeta);
mapPane.addItem(new GuiItem(mapItem, event -> {
// Handle click event if needed
// Example: navigate to the map or show map details
Player player = (Player) event.getWhoClicked();
player.sendMessage("Clicked on map: " + mapName);
}));
}
} else {
SGM.getInstance().getLogger().log(Level.WARNING, "No maps found for game: " + game);
}
}
}

View File

@ -135,6 +135,12 @@ public class LevelManager {
if (mapDir.exists()) { if (mapDir.exists()) {
FileUtils.deleteDirectory(mapDir); FileUtils.deleteDirectory(mapDir);
} }
File configFile = new File(levelDir + "/sgm.yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
String gameIntName = config.getString("game");
if (SGM.getInstance().getGameManager().getRegisteredGames().contains(gameIntName)) {
enabledMaps.get(gameIntName).add(name);
}
FileUtils.copyDirectory(levelDir, mapDir); FileUtils.copyDirectory(levelDir, mapDir);
FileUtils.delete(new File(mapDir + "/uid.dat")); FileUtils.delete(new File(mapDir + "/uid.dat"));
loadWorld(name, LevelType.LEVEL); loadWorld(name, LevelType.LEVEL);

View File

@ -0,0 +1,19 @@
<!--
~ Created by 2vb - 4/7/2024
-->
<chestgui title="New Game" rows="3" onGlobalClick="globalClick">
<outlinepane x="0" y="0" length="9" height="3" priority="lowest" repeat="true">
<item id="black_stained_glass_pane">
<displayname> </displayname>
</item>
</outlinepane>
<outlinepane x="3" y="1" length="3" height="1" gap="1">
<item id="nether_brick" onClick="initGame">
<displayname>Capture The Brick</displayname>
</item>
<item id="command_block" onClick="initGame">
<displayname>TestGame</displayname>
</item>
</outlinepane>
</chestgui>

View File

@ -0,0 +1,9 @@
<!--
~ Created by 2vb - 4/7/2024
-->
<chestgui title="Maps" rows="5" onGlobalClick="globalClick">
<outlinepane x="0" y="0" length="9" height="5" priority="lowest" repeat="true">
<item id="black_stained_glass_pane" />
</outlinepane>
</chestgui>