Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Fix #47, with some changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNekoDev committed Sep 9, 2023
1 parent 7d60f2c commit 374a3b8
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 91 deletions.
2 changes: 1 addition & 1 deletion astralbooks-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.8.0</version>
<version>5.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import lombok.Getter;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.luckperms.api.LuckPerms;
Expand Down Expand Up @@ -74,9 +75,9 @@
@SuppressWarnings("RegExpRedundantEscape")
public class AstralBooksCore implements AstralBooksAPI {
private final AstralBooksPlugin plugin;
private Distribution distribution = null;
@Getter private Distribution distribution = null;
private final Map<Chunk, Set<Block>> blocksPairedToChunk = new HashMap<>();
private final Map<Block, PairTuple<ItemStack, ItemStack>> clickableBlocks = new HashMap<>();
@Getter private final Map<Block, PairTuple<ItemStack, ItemStack>> clickableBlocks = new HashMap<>();
private final Pattern namePattern = Pattern.compile("^[a-zA-Z0-9_-]+$");
private final Pattern permissionPattern = Pattern.compile("^[a-zA-Z0-9\\._-]+$");

Expand Down Expand Up @@ -169,13 +170,9 @@ public Map<Block, PairTuple<ItemStack, ItemStack>> getBlocksEntriesPairedToChunk
return reducedMap;
}

public Map<Block, PairTuple<ItemStack, ItemStack>> getClickableBlocks() {
return this.clickableBlocks;
}

@Override
public ItemStack getBookOfBlock(Block block, Side side) {
PairTuple<ItemStack, ItemStack> pairBook = clickableBlocks.get(block);
PairTuple<ItemStack, ItemStack> pairBook = this.clickableBlocks.get(block);
if (pairBook == null)
return null;
return switch (side) {
Expand Down Expand Up @@ -280,20 +277,18 @@ public ItemData itemDataFactory(ItemStack stack) {
}

public EntityData entityDataFactory(Entity entity) {
if (!this.distribution.isNBTAPIRequired()) {
if (this.distribution.isPersistentDataContainerEnabled())
return new PersistentEntityData(entity);
} else if (this.plugin.isNBTAPIEnabled()) {
else if (this.plugin.isNBTAPIEnabled())
return new NBTAPIEntityData(entity);
}
return new EmptyEntityData();
}

public ChunkData chunkDataFactory(Chunk chunk) {
if (!this.distribution.isNBTAPIRequired()) {
if (this.distribution.isPersistentDataContainerEnabled())
return new PersistentChunkData(chunk);
} else if (this.plugin.isNBTAPIEnabled()) {
else if (this.plugin.isNBTAPIEnabled())
return new NBTAPIChunkData(chunk);
}
return new EmptyChunkData();
}

Expand Down Expand Up @@ -359,11 +354,6 @@ public boolean isValidPermission(String permission) {
return this.permissionPattern.matcher(permission).matches();
}

public Distribution getDistribution() {
return this.distribution;
}


@Override
public boolean openBook(Player player, ItemStack book) {
player.closeInventory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package ro.niconeko.astralbooks.listeners;

import lombok.Getter;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -332,19 +333,16 @@ public void onBookSign(PlayerEditBookEvent event) {
}

private static class DelayedPlayer implements Delayed {
private final long startTime = System.currentTimeMillis();
private final long startTime;
private final long maxLifeTimeMillis;
private final Player player;
@Getter private final Player player;

public DelayedPlayer(Player player, long maxLifeTimeMillis) {
this.startTime = System.currentTimeMillis();
this.maxLifeTimeMillis = maxLifeTimeMillis;
this.player = player;
}

public Player getPlayer() {
return this.player;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -369,7 +367,9 @@ private long getDelayMillis() {

@Override
public int compareTo(@NotNull Delayed that) {
return Long.compare(this.getDelayMillis(), ((DelayedPlayer) that).getDelayMillis());
if (that instanceof DelayedPlayer delayedPlayer)
return Long.compare(this.getDelayMillis(), delayedPlayer.getDelayMillis());
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void onWorldUnload(WorldUnloadEvent event) {
}

private void chunkLoad(Chunk chunk) {
if (!this.plugin.isNBTAPIEnabled() || this.api.getDistribution().isNBTAPIRequired())
if (!(this.api.getDistribution().isPersistentDataContainerEnabled() || this.plugin.isNBTAPIEnabled()))
return;
try {
this.api.deployBooksForChunk(chunk, this.api.chunkDataFactory(chunk).loadChunk(this.distribution));
Expand All @@ -85,7 +85,7 @@ private void chunkLoad(Chunk chunk) {
}

private void chunkUnload(Chunk chunk) {
if (!this.plugin.isNBTAPIEnabled() || !this.api.getDistribution().isNBTAPIRequired())
if (!(this.api.getDistribution().isPersistentDataContainerEnabled() || this.plugin.isNBTAPIEnabled()))
return;
try {
this.api.chunkDataFactory(chunk).unloadChunk(this.distribution, this.api);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@

package ro.niconeko.astralbooks.persistent;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class BlocksToBooksPairs {
private final List<BlockToBookPair> list = new ArrayList<>();

public List<BlockToBookPair> getList() {
return this.list;
}

public void add(BlockToBookPair blockToBookPair) {
this.list.add(blockToBookPair);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package ro.niconeko.astralbooks.persistent;

import lombok.Getter;
import org.bukkit.persistence.PersistentDataType;

@SuppressWarnings("rawtypes")
@Getter
public enum NBTDataType {
BYTE(PersistentDataType.BYTE),
BYTE_ARRAY(PersistentDataType.BYTE_ARRAY),
Expand All @@ -40,7 +42,4 @@ public enum NBTDataType {
this.type = type;
}

public PersistentDataType getType() {
return this.type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import com.google.gson.JsonObject;
import de.tr7zw.nbtapi.NBTChunk;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTCompoundList;
import de.tr7zw.nbtapi.NBTContainer;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import org.bukkit.Chunk;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import ro.niconeko.astralbooks.AstralBooksCore;
import ro.niconeko.astralbooks.dist.Distribution;
import ro.niconeko.astralbooks.utils.PersistentKey;
Expand All @@ -45,12 +46,12 @@ public NBTAPIChunkData(Chunk chunk) {
@Override
public Map<Block, PairTuple<ItemStack, ItemStack>> loadChunk(Distribution distribution) throws IllegalAccessException {
Map<Block, PairTuple<ItemStack, ItemStack>> chunkBlocks = new HashMap<>();
if (nbtChunk.getPersistentDataContainer().hasKey(PersistentKey.CHUNK_TAG.getValue())) {
if (nbtChunk.getPersistentDataContainer().hasTag(PersistentKey.CHUNK_TAG.getValue())) {
NBTCompoundList blocksDataContainers = nbtChunk.getPersistentDataContainer().getCompoundList(PersistentKey.CHUNK_TAG.getValue());
for (NBTCompound blockDataContainer : blocksDataContainers) {
if (!blockDataContainer.hasKey(PersistentKey.BLOCK_LOCATION_X.getValue())
|| !blockDataContainer.hasKey(PersistentKey.BLOCK_LOCATION_Y.getValue())
|| !blockDataContainer.hasKey(PersistentKey.BLOCK_LOCATION_Z.getValue()))
for (ReadWriteNBT blockDataContainer : blocksDataContainers) {
if (!blockDataContainer.hasTag(PersistentKey.BLOCK_LOCATION_X.getValue())
|| !blockDataContainer.hasTag(PersistentKey.BLOCK_LOCATION_Y.getValue())
|| !blockDataContainer.hasTag(PersistentKey.BLOCK_LOCATION_Z.getValue()))
continue;
String leftBookJson = blockDataContainer.getString(PersistentKey.BLOCK_LEFT_BOOK.getValue());
ItemStack leftBook = (leftBookJson == null || leftBookJson.isEmpty()) ?
Expand Down Expand Up @@ -81,16 +82,22 @@ public void unloadChunk(Distribution distribution, AstralBooksCore core) throws
ItemStack right = pair.secondValue();
String leftJson = left != null ? distribution.convertBookToJson(left).toString() : null;
String rightJson = right != null ? distribution.convertBookToJson(right).toString() : null;
NBTContainer blockDataContainer = new NBTContainer();
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_X.getValue(), block.getX());
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_Y.getValue(), block.getY());
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_Z.getValue(), block.getZ());
if (leftJson != null)
blockDataContainer.setString(PersistentKey.BLOCK_LEFT_BOOK.getValue(), leftJson);
if (rightJson != null)
blockDataContainer.setString(PersistentKey.BLOCK_RIGHT_BOOK.getValue(), rightJson);
NBTContainer blockDataContainer = getNbtContainer(block, leftJson, rightJson);
blocksDataContainers.addCompound(blockDataContainer);
}
core.concentrateBooksForChunk(chunk);
}

@NotNull
private static NBTContainer getNbtContainer(Block block, String leftJson, String rightJson) {
NBTContainer blockDataContainer = new NBTContainer();
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_X.getValue(), block.getX());
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_Y.getValue(), block.getY());
blockDataContainer.setInteger(PersistentKey.BLOCK_LOCATION_Z.getValue(), block.getZ());
if (leftJson != null)
blockDataContainer.setString(PersistentKey.BLOCK_LEFT_BOOK.getValue(), leftJson);
if (rightJson != null)
blockDataContainer.setString(PersistentKey.BLOCK_RIGHT_BOOK.getValue(), rightJson);
return blockDataContainer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import java.util.List;
import java.util.Optional;

@Getter
public class PluginSettings extends Settings {
@Getter private boolean metricsEnabled = true;
@Getter private boolean updateCheck = true;
@Getter @Setter private boolean joinBookEnabled = false;
@Getter private boolean joinBookAlwaysShow = false;
@Getter private boolean joinBookEnableDelay = false;
@Getter private int joinBookDelay = 0;
@Getter private boolean bookSignSecurityEnabled = false;
private boolean metricsEnabled = true;
private boolean updateCheck = true;
@Setter private boolean joinBookEnabled = false;
private boolean joinBookAlwaysShow = false;
private boolean joinBookEnableDelay = false;
private int joinBookDelay = 0;
private boolean bookSignSecurityEnabled = false;

public PluginSettings(AstralBooksPlugin plugin) {
super(plugin);
Expand All @@ -60,10 +61,8 @@ public void load(ConfigurationSection section) {
}

@NonNull
@Getter
private final StorageSettings storageSettings = new StorageSettings(super.plugin);

@NonNull
@Getter
private final MessageSettings messageSettings = new MessageSettings(super.plugin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
Expand All @@ -46,7 +47,7 @@

public class PluginStorage {
private final AstralBooksPlugin plugin;
private StorageCache cache;
@Getter private StorageCache cache;
private Storage storage;
private final File joinBookFile;
private JsonObject joinBookDatabase;
Expand Down Expand Up @@ -126,10 +127,6 @@ public void unload() {
this.storage.unload();
}

public StorageCache getCache() {
return this.cache;
}

private JsonObject readJsonFile(File jsonFile) throws JsonParseException {
try (FileReader fileReader = new FileReader(jsonFile)) {
return AstralBooksCore.PRETTY_GSON.fromJson(fileReader, JsonObject.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package ro.niconeko.astralbooks.storage;

import lombok.Getter;

public enum StorageType {
JSON("json", "Json"),
MYSQL("mysql", "MySQL"),
Expand All @@ -25,7 +27,7 @@ public enum StorageType {
MARIADB("mariadb", "MariaDB");

private final String type;
private final String formattedName;
@Getter private final String formattedName;

StorageType(String type, String formattedName) {
this.type = type;
Expand All @@ -37,10 +39,6 @@ public String toString() {
return this.type;
}

public String getFormattedName() {
return formattedName;
}

public static StorageType fromString(String type) {
if (type.equalsIgnoreCase("mysql"))
return MYSQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
import java.util.List;
import java.util.Optional;

@Getter
public class StorageEmbedSettings extends Settings {
@Getter
private String fileName = "database";
@Getter
private boolean EncryptionEnabled = false;
@Getter
private int saveInterval = 60;

public StorageEmbedSettings(AstralBooksPlugin plugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
import java.util.List;
import java.util.Optional;

@Getter
public class StorageRemoteSettings extends Settings {
@Getter private String host = "localhost";
@Getter private int port = 3306;
@Getter private String database = "astralbooks";
@Getter private String username = "root";
@Getter private String password = "";
@Getter private boolean SSLEnabled = false;
@Getter private String tablePrefix = "abooks_";
@Getter private String serverName = "default";
private String host = "localhost";
private int port = 3306;
private String database = "astralbooks";
private String username = "root";
private String password = "";
private boolean SSLEnabled = false;
private String tablePrefix = "abooks_";
private String serverName = "default";

public StorageRemoteSettings(AstralBooksPlugin plugin) {
super(plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import java.util.List;
import java.util.Optional;

@Getter
public class StorageSettings extends Settings {
@Getter private StorageType databaseType = StorageType.H2;
@Getter private int databaseThreads = 2;
@Getter private boolean securityBookPurgeEnabled = true;
@Getter private int securityBookPurgeOlderThan = 30;
@Getter private final StorageRemoteSettings RemoteSettings = new StorageRemoteSettings(super.plugin);
@Getter private final StorageEmbedSettings EmbedSettings = new StorageEmbedSettings(super.plugin);
private StorageType databaseType = StorageType.H2;
private int databaseThreads = 2;
private boolean securityBookPurgeEnabled = true;
private int securityBookPurgeOlderThan = 30;
private final StorageRemoteSettings RemoteSettings = new StorageRemoteSettings(super.plugin);
private final StorageEmbedSettings EmbedSettings = new StorageEmbedSettings(super.plugin);

public StorageSettings(AstralBooksPlugin plugin) {
super(plugin);
Expand Down
Loading

0 comments on commit 374a3b8

Please sign in to comment.