Skip to content

Commit

Permalink
Add server utils
Browse files Browse the repository at this point in the history
Added server utils
Added item utils
Added
  • Loading branch information
DavidOut03 committed Jun 15, 2024
1 parent 1dce9ae commit 5b19ee2
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 82 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>mcapi.davidout</groupId>
<artifactId>spigot-tool-kit</artifactId>
<version>1.0.0</version>
<version>1.0.5-SNAPSHOT</version>
<packaging>jar</packaging>


Expand Down Expand Up @@ -70,6 +70,11 @@
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
</dependency>

</dependencies>

Expand Down
49 changes: 0 additions & 49 deletions src/main/java/mcapi/davidout/Main.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/java/mcapi/davidout/Test.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public <T> T loadFile(Class<T> fileClass, String path) throws IOException {

@Override
public <T> boolean saveFile(T fileObject, String path) throws IOException {
if(fileObject == null) {
return false;
}

try (FileWriter writer = new FileWriter(formatPath(path) )) {
gson.toJson(fileObject, writer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public <T> T loadFile(Class<T> fileClass, String path) throws IOException {

@Override
public <T> boolean saveFile(T fileObject, String path) throws IOException {
if(fileObject == null) {
return false;
}

objectMapper.writeValue(new File(formatPath(path)), fileObject);
return true;
}
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/mcapi/davidout/manager/gui/GuiManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mcapi.davidout.manager.gui;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;

import java.util.*;
Expand All @@ -25,6 +27,10 @@ public List<IGui> getGuis() {
return this.guis;
}

public void openGui(IGui gui, Player player, Object ...args) {
gui.openInventory(player, args);
}


@EventHandler
public void onClick(InventoryClickEvent e) {
Expand All @@ -34,7 +40,7 @@ public void onClick(InventoryClickEvent e) {

Inventory topInventory = e.getView().getTopInventory();
if (!(topInventory.getHolder() instanceof GuiHolder)) {
return; // or handle the case when the holder is not a GuiHolder
return;
}

GuiHolder holder = (GuiHolder) topInventory.getHolder();
Expand All @@ -43,14 +49,37 @@ public void onClick(InventoryClickEvent e) {
).findFirst()
.orElse(null);

if(openGui == null) {
if(openGui == null || e.getClickedInventory() == null) {
return;
}


openGui.onClick(e);
}

@EventHandler
public void onClose(InventoryCloseEvent e) {
if(e.getInventory() == null) {
return;
}

Inventory topInventory = e.getInventory();
if (!(topInventory.getHolder() instanceof GuiHolder)) {
return;
}

GuiHolder holder = (GuiHolder) topInventory.getHolder();
IGui openGui = this.guis.stream().filter(gui ->
gui.getUUID().equals(holder.getInventoryUUID())
).findFirst()
.orElse(null);

if(openGui == null) {
return;
}

openGui.onClose(e);
}




Expand Down
39 changes: 32 additions & 7 deletions src/main/java/mcapi/davidout/manager/gui/IGui.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package mcapi.davidout.manager.gui;

import mcapi.davidout.utils.TextUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.UUID;


Expand All @@ -19,16 +23,34 @@ public IGui() {
this.identifier = UUID.randomUUID();
}

public void openInventory(Player p, Object... arguments) {
Inventory inventory = this.createInventory(
Bukkit.createInventory(new GuiHolder(this.identifier), this.getRows() * 9, this.getTitle(p, arguments)),
p,
arguments
);
public void openInventory(Player player, Object... arguments) {
if (player == null) {
return;
}

Inventory inventory = Bukkit.createInventory(new GuiHolder(this.identifier), this.getRows() * 9, TextUtils.formatColorCodes(this.getTitle(player, arguments)));
Inventory customInventory = this.createInventory(inventory, player, arguments);

if (customInventory != null) {
inventory = customInventory;
}

updateInventory(customInventory);
player.openInventory(inventory);
}

public void updateInventory(Inventory inventory) {

p.openInventory(inventory);
}

public void fillInventory(Inventory inventory, ItemStack itemStack) {
for(int i = 0; i < (this.getRows() * 9); i++) {
inventory.setItem(i, itemStack);
}
}



public boolean isGui(Inventory inventory) {
return inventory.getHolder() instanceof GuiHolder && ((GuiHolder)inventory).getInventoryUUID().equals(this.identifier);
}
Expand All @@ -40,6 +62,9 @@ public boolean isGui(Inventory inventory) {
public abstract Inventory createInventory(Inventory inventory, Player player, Object... objects);

public abstract void onClick(InventoryClickEvent e);
public void onClose(InventoryCloseEvent e) {

}

public UUID getUUID() {
return this.identifier;
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/mcapi/davidout/manager/language/MessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ public String getMessageFromBundle(IMessageBundle bundle, String key) {
return "No bundle selected";
}

return
bundle.getMessages().entrySet().stream().
filter(entry -> entry.getKey().equalsIgnoreCase(key)).findFirst().map(Map.Entry::getValue).orElse("&cCould not find the message.");
return TextUtils.formatColorCodes(
bundle.getMessages().entrySet().stream().
filter(
entry -> entry.getKey().equalsIgnoreCase(key)
).findFirst()
.map(Map.Entry::getValue)
.orElse("&cCould not find the %message.".replace("%message", key))

.replace("%prefix", bundle.getPrefix())
);
}

@Override
Expand Down Expand Up @@ -116,14 +123,11 @@ public boolean loadMessageBundles() {
.findFirst();

existingBundle.ifPresent(bundle -> {
System.out.println("Removing existing bundle: " + bundle.getName());
messageBundles.remove(bundle);
});

System.out.println("Adding new bundle: " + bundleName);
messageBundles.add(new MessageBundle(bundleName, messages));
messageBundles.add(new MessageBundle(bundleName, messages, savedBundle.prefix));
} catch (IOException e) {
System.err.println("Error loading message bundles: " + e.getMessage());
e.printStackTrace();
}
}
Expand All @@ -136,7 +140,7 @@ public boolean loadMessageBundles() {
public boolean saveMessageBundles() {
for (IMessageBundle bundle : this.messageBundles) {
try {
fileManager.saveFile(new SavedBundle(bundle.getName(), bundle.getMessages()), bundle.getName());
fileManager.saveFile(new SavedBundle(bundle.getName(), bundle.getMessages(), bundle.getPrefix()), bundle.getName());
} catch (Exception ex) {
ex.printStackTrace();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

public interface IMessageBundle {
String getName();
String getPrefix();
Map<String, String> getMessages();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
public class MessageBundle implements IMessageBundle {

private final String name;
private final String prefix;
private final Map<String, String> messages;

public MessageBundle() {
this.name = "";
this.messages = new HashMap<>();
this.prefix = "";
}

public MessageBundle(String name, Map<String, String> messages) {
public MessageBundle(String name, Map<String, String> messages, String prefix) {
this.name = name;
this.messages = messages;
this.prefix = prefix;
}


Expand All @@ -24,6 +27,11 @@ public String getName() {
return name;
}

@Override
public String getPrefix() {
return prefix;
}

@Override
public Map<String, String> getMessages() {
return messages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
public class SavedBundle {

public String name;
public String prefix;
public Map<String, String> messages;

public SavedBundle(String name, Map<String, String> messages) {
public SavedBundle(String name, Map<String, String> messages, String prefix) {
this.name = name;
this.messages = messages;
this.prefix = prefix;
}

public SavedBundle() {
this.messages = new HashMap<>();
this.name = UUID.randomUUID().toString();
this.prefix = "";
}
}

Loading

0 comments on commit 5b19ee2

Please sign in to comment.