Skip to content

Commit

Permalink
A
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhincore committed Jun 9, 2022
1 parent c2f55eb commit 76757eb
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 33 deletions.
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# TODO

[X] Proper file names
[ ] Use ACF?
[ ] Better separation of concerns
[ ] Proper (de)serialization
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ repositories {
dependencies {
compileOnly 'io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT'
compileOnly 'de.jeff_media:ChestSortAPI:13.0.0-SNAPSHOT'
implementation 'co.aikar:taskchain-bukkit:3.7.2'
implementation 'co.aikar:acf-paper:0.5.1-SNAPSHOT'
}

group = 'eu.zhincore.chestnetworks'
version = '1.0-SNAPSHOT'
description = 'chestnetworks'

shadowJar {
relocate 'co.aikar.taskchain', 'eu.zhincore.chestnetworks.taskchain'
relocate 'co.aikar.commands', 'eu.zhincore.chestnetworks.acf'
relocate 'co.aikar.locales', 'eu.zhincore.chestnetworks.locales'
archiveClassifier = "shadowed"
}

compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/eu/zhincore/chestnetworks/ChestNetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eu.zhincore.chestnetworks;

import co.aikar.commands.BaseCommand;

public class ChestNetCommand extends BaseCommand {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class NetworksController {
public class ChestNetController {
private HashMap<String, String[]> playerSelectQueue = new HashMap<>();
private ChestNetworks plugin;
private Database database;
private ChestNetworksPlugin plugin;
private ChestNetDatabase database;
private JSONObject players;

public NetworksController(ChestNetworks plugin) {
public ChestNetController(ChestNetworksPlugin plugin) {
this.plugin = plugin;
database = new Database(plugin);
database = new ChestNetDatabase(plugin);
players = (JSONObject) database.data;
}

Expand Down Expand Up @@ -111,7 +111,8 @@ public List<Location> listGlobalChestLocations() {
for (String playerId : playerIds) {
for (String networkName : listNets(playerId)) {
output.addAll(listChests(playerId, networkName).stream()
.map(v -> ((Location) Database.jsonToLoc((JSONObject) v.get("location")))).collect(Collectors.toList()));
.map(v -> ((Location) ChestNetDatabase.jsonToLoc((JSONObject) v.get("location"))))
.collect(Collectors.toList()));
}
}
return output;
Expand All @@ -128,7 +129,6 @@ public void delete(Player player, String name) {
return;
}
}
plugin.messenger.send("cnet404", player, name);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -200,7 +200,7 @@ public void onPlayerSelected(Player player, Location loc) {
@SuppressWarnings("unchecked")
private void addChest(Player player, Location loc, String[] playerData) {
String playerId = player.getUniqueId().toString();
JSONObject jsonLoc = (JSONObject) Database.locToJson(loc);
JSONObject jsonLoc = (JSONObject) ChestNetDatabase.locToJson(loc);
JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(playerData[1]);

// Detect is this chest is already registered
Expand Down Expand Up @@ -238,7 +238,7 @@ private void checkChest(Player player, Location loc) {

public JSONObject getChestData(Location loc) {
for (Object chest : listGlobalChests()) {
if (Database.jsonToLoc((JSONObject) ((JSONObject) chest).get("location")).equals(loc)) {
if (ChestNetDatabase.jsonToLoc((JSONObject) ((JSONObject) chest).get("location")).equals(loc)) {
return (JSONObject) chest;
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public void update(String playerId, String netName) {
JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(netName);

for (Object chestData : network) {
Chest chest = getChestByLoc(Database.jsonToLoc((JSONObject) ((JSONObject) chestData).get("location")));
Chest chest = getChestByLoc(ChestNetDatabase.jsonToLoc((JSONObject) ((JSONObject) chestData).get("location")));
if (chest != null) {
update(chest.getInventory());
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public void update(Inventory inventory) {
for (Object inputChestData : network) {
if (((JSONObject) inputChestData).get("type").equals("input")) {
Chest inputChest = getChestByLoc(
Database.jsonToLoc((JSONObject) ((JSONObject) inputChestData).get("location")));
ChestNetDatabase.jsonToLoc((JSONObject) ((JSONObject) inputChestData).get("location")));
if (inputChest != null) {
update(inputChest.getInventory());
}
Expand All @@ -317,7 +317,8 @@ public void update(Inventory inventory) {
if (chestData.get("sort") != null) sortInventory(inventory);
for (var _chestData : changedChests) {
if (_chestData == null || _chestData.get("sort") != null) continue;
Chest _chest = getChestByLoc(Database.jsonToLoc((JSONObject) ((JSONObject) _chestData).get("location")));
Chest _chest = getChestByLoc(
ChestNetDatabase.jsonToLoc((JSONObject) ((JSONObject) _chestData).get("location")));
sortInventory(_chest.getInventory());
}
}
Expand All @@ -341,7 +342,7 @@ private JSONObject placeItems(JSONArray network, Inventory inventory, ItemStack
|| !(chestcontent.isEmpty() ? fallback : chestcontent.contains(item.getType().toString())))
continue;

Location loc = Database.jsonToLoc((JSONObject) chestconf.get("location"));
Location loc = ChestNetDatabase.jsonToLoc((JSONObject) chestconf.get("location"));
Chest target = getChestByLoc(loc);
if (target == null) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Database {
public class ChestNetDatabase {
public JSONObject data;
private File file;
private JSONParser parser = new JSONParser();

public Database(ChestNetworks plugin) {
public ChestNetDatabase(ChestNetworksPlugin plugin) {
file = new File(plugin.getDataFolder(), "data.json");
reload();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;

public class EventListener implements Listener {
private NetworksController networkController;
public class ChestNetListener implements Listener {
private ChestNetController networkController;

public EventListener(NetworksController networkController) {
public ChestNetListener(ChestNetController networkController) {
this.networkController = networkController;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Messenger {
public class ChestNetMessenger {
private static final String prefix = "§b[§2ChestNetworks§b]§r ";
private JSONObject messages;
private JSONParser parser = new JSONParser();

public Messenger(Reader messagesRaw) throws IOException, ParseException {
public ChestNetMessenger(Reader messagesRaw) throws IOException, ParseException {
messages = (JSONObject) parser.parse(messagesRaw);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.parser.ParseException;

public class ChestNetworks extends JavaPlugin {
public Messenger messenger;
private NetworksController networksController;
public class ChestNetworksPlugin extends JavaPlugin {
public ChestNetMessenger messenger;
private ChestNetController networksController;

@Override
public void onEnable() {
try {
messenger = new Messenger(getTextResource("messages.json"));
messenger = new ChestNetMessenger(getTextResource("messages.json"));
} catch (IOException | ParseException e) {
e.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
return;
}
networksController = new NetworksController(this);
networksController = new ChestNetController(this);
getCommand("chestnet").setExecutor(new CommandChestNet(this, networksController));
getCommand("chestnet").setTabCompleter(new CommandChestNetTabCompleter(networksController));
getServer().getPluginManager().registerEvents(new EventListener(networksController), this);
getServer().getPluginManager().registerEvents(new ChestNetListener(networksController), this);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/eu/zhincore/chestnetworks/CommandChestNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.bukkit.entity.Player;

public class CommandChestNet implements CommandExecutor {
private NetworksController networksController;
private ChestNetworks plugin;
private ChestNetController networksController;
private ChestNetworksPlugin plugin;

public CommandChestNet(ChestNetworks plugin, NetworksController networksController) {
public CommandChestNet(ChestNetworksPlugin plugin, ChestNetController networksController) {
this.networksController = networksController;
this.plugin = plugin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.bukkit.entity.Player;

public class CommandChestNetTabCompleter implements TabCompleter {
private NetworksController networksController;
private ChestNetController networksController;

public CommandChestNetTabCompleter(NetworksController networksController) {
public CommandChestNetTabCompleter(ChestNetController networksController) {
this.networksController = networksController;
}

Expand Down

0 comments on commit 76757eb

Please sign in to comment.