Skip to content

Commit

Permalink
Merge pull request #59 from Kwantux/dev
Browse files Browse the repository at this point in the history
Update 3.0.5
  • Loading branch information
Kwantux authored Nov 12, 2024
2 parents a754a04 + 4d4056e commit a185b08
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 149 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION


Copyright (c) 2022 - 2024 NanoFlux
Copyright (c) 2022 - 2024 Kwantux


1. Definitions.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You host a Minecraft-Server with paper and have issues with large sorting system

[Modrinth](https://modrinth.com/plugin/networks)

[Hangar](https://hangar.papermc.io/NanoFlux/Networks)
[Hangar](https://hangar.papermc.io/Kwantux/Networks)

[Bukkit](https://dev.bukkit.org/projects/networks)

Expand Down Expand Up @@ -50,7 +50,7 @@ To remove a chest from your network, simply just break it
## More information

Most commands and their functionalities can be seen in <code>/networks help</code>
See a more detailed instruction at the [wiki](https://github.com/nanoflux/networks/wiki)
See a more detailed instruction at the [wiki](https://github.com/Kwantux/networks/wiki)


## Installation
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
runPaper.folia.registerTask()

group = "de.kwantux"
version = "3.0.4"
version = "3.0.5"
description = "A performance friendly way to sort your items"

repositories {
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/de/kwantux/networks/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.kwantux.networks;

import de.kwantux.networks.commands.NetworksCommandManager;
import de.kwantux.networks.component.util.FilterTranslator;
import de.kwantux.networks.event.NoticeListener;
import de.kwantux.networks.utils.DoubleChestUtils;
import de.kwantux.networks.utils.FoliaUtils;
import de.kwantux.networks.utils.Metrics;
Expand All @@ -24,6 +26,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;


Expand Down Expand Up @@ -90,12 +93,20 @@ public void onEnable() {

lang = new LanguageController(this, cfg.getLanguage(), "en", "de");



new NetworksCommandManager(this);

regionScheduler = getServer().getRegionScheduler();
globalRegionScheduler = getServer().getGlobalRegionScheduler();
asyncScheduler = getServer().getAsyncScheduler();

try {
FilterTranslator.load(Path.of(getDataFolder().getAbsolutePath(), "filters.txt"));
} catch (IOException e) {
throw new RuntimeException(e);
}

mgr = new Manager(this);
crf = new CraftingManager(this);

Expand All @@ -113,10 +124,11 @@ public void onEnable() {
mgr.loadData();

new ComponentListener(this);
new BlockPlaceListener(this, dcu);
new BlockBreakListener(this, dcu);
new WandListener(this, crf, dcu);
new BlockPlaceListener(this);
new BlockBreakListener(this);
new WandListener(this);
new PlayerJoinListener(this);
new NoticeListener(this);

if (FoliaUtils.folia) {
logger.warning("Folia support on Networks is still in beta, please report any bugs.");
Expand All @@ -128,6 +140,9 @@ public void onEnable() {
}
}

if (Config.autoSaveInterval > 0)
asyncScheduler.runAtFixedRate(this, (t) -> mgr.saveData(), Config.autoSaveInterval, Config.autoSaveInterval, TimeUnit.SECONDS);

if (cfg.logoOnLaunch()) logger.info(startMessage);

}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/kwantux/networks/Manager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.kwantux.networks;


import de.kwantux.networks.component.util.FilterTranslator;
import de.kwantux.networks.utils.DoubleChestUtils;
import de.kwantux.networks.component.ComponentType;
import de.kwantux.networks.component.NetworkComponent;
Expand All @@ -12,6 +13,7 @@
import org.bukkit.persistence.PersistentDataContainer;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;

Expand Down Expand Up @@ -102,7 +104,11 @@ public void saveData() {
for (String id : networks.keySet()) {
storage.saveNetwork(id, networks.get(id));
}
logger.info("Saved " + networks.size() + " Networks");
try {
FilterTranslator.save();
} catch (IOException e) {
throw new RuntimeException(e);
}
}


Expand Down
24 changes: 16 additions & 8 deletions src/main/java/de/kwantux/networks/commands/NetworksCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.kwantux.networks.component.component.InputContainer;
import de.kwantux.networks.component.component.MiscContainer;
import de.kwantux.networks.component.component.SortingContainer;
import de.kwantux.networks.component.util.FilterTranslator;
import de.kwantux.networks.config.Config;
import de.kwantux.networks.inventory.InventoryMenuManager;
import de.kwantux.networks.utils.BlockLocation;
Expand All @@ -33,7 +34,9 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import static de.kwantux.networks.Main.*;
import static de.kwantux.networks.commands.ComponentTypeParser.componentTypeParser;
Expand Down Expand Up @@ -472,16 +475,11 @@ private void components(CommandContext<CommandSender> context) {
if (network == null) return;

if (network.components().isEmpty()) {
lang.message(sender, "component.list.empty");
lang.message(sender, "component.list.empty", network.name());
return;
}

for (NetworkComponent component : network.components()) {
System.out.println(component);
System.out.println(component.type());
System.out.println(component.pos());
System.out.println(component.type().tag());

sender.sendMessage(
Objects.requireNonNullElse(lang.getFinal("item.name.component."+component.type().tag()), Component.text("Unknown Component type: " + component.type().tag()))
.append(Component.text(": ")
Expand All @@ -503,8 +501,18 @@ public static Component componentInfo(Network network, @Nullable NetworkComponen
return switch (component) {
case InputContainer container ->
lang.get("wand.info.input", network.name(), component.pos().toString(), String.valueOf(container.range()));
case SortingContainer container ->
lang.get("wand.info.sorting", network.name(), component.pos().toString(), String.valueOf(container.acceptorPriority()), Arrays.stream(container.filters()).toList().toString());
case SortingContainer container -> {
Component filters = Component.text("[");
Set<Component> filterSet = Arrays.stream(container.filters()).mapToObj(
FilterTranslator::translate
).collect(Collectors.toSet());
for (Component filter : filterSet) {
filters = filters.append(filter);
filters = filters.append(Component.text(", "));
}
filters = filters.append(Component.text("]"));
yield lang.get("wand.info.sorting", network.displayText(), component.pos().displayText(), Component.text(String.valueOf(container.acceptorPriority())), filters);
}
case MiscContainer container ->
lang.get("wand.info.misc", network.name(), component.pos().toString(), String.valueOf(container.acceptorPriority()));
case null, default -> Component.empty();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/de/kwantux/networks/compat/LegacyNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import de.kwantux.networks.component.component.MiscContainer;
import de.kwantux.networks.utils.BlockLocation;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -90,7 +91,11 @@ record LegacySortingContainer(
) {
public @Nullable SortingContainer convert() {
if (pos == null) return null;
return new SortingContainer(pos.convert(), items, priority);
int[] filters = new int[items.length];
for (int i = 0; i < items.length; i++) {
filters[i] = Objects.requireNonNullElse(Material.getMaterial(items[i]), Material.AIR).ordinal();
}
return new SortingContainer(pos.convert(), SortingContainer.convertLegacyFilters(items), priority);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/de/kwantux/networks/component/NetworkComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -71,7 +72,11 @@ public static ItemStack item(ComponentType type, Map<String, Object> properties)
List<Component> lore = Main.lang.getItemLore("component." + type.tag());
if (Config.propertyLore)
for (Map.Entry<String, Object> entry : properties.entrySet()) {
lore.add(Main.lang.getFinal("property." + entry.getKey()).append(Component.text(": " + entry.getValue())));
String value = String.valueOf(entry.getValue());
if (entry.getValue() instanceof int[]) value = Arrays.toString((int[]) entry.getValue());
if (entry.getValue() instanceof long[]) value = Arrays.toString((long[]) entry.getValue());
if (entry.getValue() instanceof byte[]) value = Arrays.toString((byte[]) entry.getValue());
lore.add(Main.lang.getFinal("property." + entry.getKey()).append(Component.text(": " + value)));
}
meta.lore(lore);
} catch (InvalidNodeException e) {
Expand All @@ -88,7 +93,7 @@ public static ItemStack item(ComponentType type, Map<String, Object> properties)
* Copies all properties from the map to the persistent data container
* @param container The persistent data container to edit
* @param map The map of properties
* @throws IllegalArgumentException Supported data types are String, Integer, Long, Double, Float, Short, Byte, Boolean, Integer[], Long[], Byte[]
* @throws IllegalArgumentException Supported data types are String, Integer, Long, Double, Float, Short, Byte, Boolean, int[], long[], byte[]
*/
private static void mapToContainer(PersistentDataContainer container, Map<String,Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand Down Expand Up @@ -118,13 +123,13 @@ else if (value instanceof Byte) {
else if (value instanceof Boolean) {
container.set(NamespaceUtils.key(key), PersistentDataType.BYTE, (boolean) value ? (byte) 1 : (byte) 0);
}
else if (value instanceof Integer[]) {
else if (value instanceof int[]) {
container.set(NamespaceUtils.key(key), PersistentDataType.INTEGER_ARRAY, (int[]) value);
}
else if (value instanceof Long[]) {
else if (value instanceof long[]) {
container.set(NamespaceUtils.key(key), PersistentDataType.LONG_ARRAY, (long[]) value);
}
else if (value instanceof Byte[]) {
else if (value instanceof byte[]) {
container.set(NamespaceUtils.key(key), PersistentDataType.BYTE_ARRAY, (byte[]) value);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import de.kwantux.networks.utils.BlockLocation;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.IntStream;

public class SortingContainer extends NetworkComponent implements Acceptor, Supplier {

Expand All @@ -25,26 +28,45 @@ public ComponentType type() {
return type;
}

private String[] filters;
private int acceptorPriority = 10;
private int supplierPriority = 0;
private int[] filters;
private int acceptorPriority;
private int supplierPriority;

public static SortingContainer create(BlockLocation pos, PersistentDataContainer container) {
if (container == null) return new SortingContainer(pos, new String[0], 10);
if (container == null) return new SortingContainer(pos, new int[0], 10, 0);

int[] filters;

try {
filters = Objects.requireNonNull(container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.INTEGER_ARRAY)); // Normal deserialization
} catch (Exception e) {
filters = convertLegacyFilters( // If deserialization fails, try legacy deserialization
Objects.requireNonNullElse(
Objects.requireNonNullElse( // Try legacy deserialization
container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.STRING),
null).split(","),
new String[0]) // If legacy deserialization fails, use empty array
);
}

// Remove all duplicates and 0 values from filters
filters = Arrays.stream(filters).distinct().filter(i -> i != 0).toArray();

return new SortingContainer(pos,
Objects.requireNonNullElse(container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.STRING).split(","), new String[0]),
filters,
Objects.requireNonNullElse(container.get(NamespaceUtils.ACCEPTOR_PRIORITY.key(), PersistentDataType.INTEGER), 10),
Objects.requireNonNullElse(container.get(NamespaceUtils.SUPPLIER_PRIORITY.key(), PersistentDataType.INTEGER), 0)
);
}

public SortingContainer(BlockLocation pos, String[] filters, int acceptorPriority) {
public SortingContainer(BlockLocation pos, int[] filters, int acceptorPriority) {
super(pos);
this.filters = filters;
this.acceptorPriority = acceptorPriority;
this.supplierPriority = 0;
}

public SortingContainer(BlockLocation pos, String[] filters, int acceptorPriority, int supplierPriority) {
public SortingContainer(BlockLocation pos, int[] filters, int acceptorPriority, int supplierPriority) {
super(pos);
this.filters = filters;
this.acceptorPriority = acceptorPriority;
Expand All @@ -54,7 +76,7 @@ public SortingContainer(BlockLocation pos, String[] filters, int acceptorPriorit
private static Map<String, Object> defaultProperties = new HashMap<>();

static {
defaultProperties.put(NamespaceUtils.FILTERS.name, "");
defaultProperties.put(NamespaceUtils.FILTERS.name, new int[0]);
defaultProperties.put(NamespaceUtils.ACCEPTOR_PRIORITY.name, 10);
defaultProperties.put(NamespaceUtils.SUPPLIER_PRIORITY.name, 0);
}
Expand All @@ -77,14 +99,18 @@ public static ComponentType register() {
@Override
public boolean fillMissingData() {
if (filters == null)
filters = new String[0];

filters = new int[0];
return pos != null;
}

@Override
public boolean accept(@Nonnull ItemStack stack) {
return Arrays.stream(filters).anyMatch(stack.getType().toString()::equalsIgnoreCase);
int matHash = stack.getItemMeta().hashCode();
int metaHash = stack.getItemMeta().hashCode();
for (int filter : filters) {
if (matHash == filter || metaHash == filter) return true;
}
return false;
}

public int acceptorPriority() {
Expand All @@ -111,7 +137,7 @@ public int supplierPriority() {
return supplierPriority;
}

public String[] filters() {
public int[] filters() {
return filters;
}

Expand All @@ -121,16 +147,24 @@ public Map<String, Object> properties() {
return new HashMap<>() {{
put(NamespaceUtils.ACCEPTOR_PRIORITY.name, acceptorPriority);
put(NamespaceUtils.SUPPLIER_PRIORITY.name, supplierPriority);
put(NamespaceUtils.FILTERS.name, String.join(",", filters));
put(NamespaceUtils.FILTERS.name, filters);
}};
}

public void addFilter(String material) {
public void addFilter(int filter) {
filters = Arrays.copyOf(filters, filters.length + 1);
filters[filters.length - 1] = material;
filters[filters.length - 1] = filter;
}

public void removeFilter(int filter) {
filters = ArrayUtils.removeElement(filters, filter);
}

public void removeFilter(String material) {
filters = ArrayUtils.removeElement(filters, material);
public static int[] convertLegacyFilters(String[] filters) {
int[] newFilters = new int[filters.length];
for (int i = 0; i < filters.length; i++) {
newFilters[i] = Objects.requireNonNullElse(Material.getMaterial(filters[i]), Material.AIR).ordinal();
}
return newFilters;
}
}
Loading

0 comments on commit a185b08

Please sign in to comment.