Skip to content

Commit

Permalink
Merge pull request #821 from SlimefunGuguProject/feat/universal-inven…
Browse files Browse the repository at this point in the history
…tory

通用数据
  • Loading branch information
StarWishsama authored Nov 9, 2024
2 parents 78328f1 + 711d351 commit 091919e
Show file tree
Hide file tree
Showing 74 changed files with 2,188 additions and 482 deletions.
8 changes: 0 additions & 8 deletions .mvn/maven-git-versioning-extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
${commit.short}-Insider
</version>
</ref>
<ref type="branch">
<pattern>test/(.+)</pattern>
<version>${ref}-${commit.short}</version>
</ref>
<ref type="branch">
<pattern>feat/(.+)</pattern>
<version>${ref}-${commit.short}</version>
</ref>
<ref type = "tag">
<pattern>([0-9].*)></pattern>
<version>${1}</version>
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.24.1</version>
<scope>provided</scope>
</dependency>

<!-- Shaded packages -->
<dependency>
<groupId>com.github.StarWishsama.dough</groupId>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/city/norain/slimefun4/SlimefunExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import city.norain.slimefun4.compatibillty.VersionedEvent;
import city.norain.slimefun4.listener.SlimefunMigrateListener;
import city.norain.slimefun4.utils.EnvUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;
import io.github.bakedlibs.dough.versions.MinecraftVersion;
import io.github.bakedlibs.dough.versions.UnknownServerVersionException;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import lombok.Getter;
import org.apache.logging.log4j.core.config.Configurator;

public final class SlimefunExtended {
private static SlimefunMigrateListener migrateListener = new SlimefunMigrateListener();
Expand All @@ -22,6 +26,12 @@ public final class SlimefunExtended {
private static void checkDebug() {
if ("true".equals(System.getProperty("slimefun.database.debug"))) {
databaseDebugMode = true;

Slimefun.getSQLProfiler().start();
} else {
Configurator.setLevel(HikariConfig.class.getName(), org.apache.logging.log4j.Level.OFF);
Configurator.setLevel(HikariDataSource.class.getName(), org.apache.logging.log4j.Level.OFF);
Configurator.setLevel(HikariPool.class.getName(), org.apache.logging.log4j.Level.OFF);
}
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/city/norain/slimefun4/api/menu/UniversalMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package city.norain.slimefun4.api.menu;

import java.util.UUID;
import javax.annotation.Nonnull;
import lombok.Getter;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;

/**
* This class represents a universal chest menu
* which a menu located by certain identify id instead of location.
*/
public class UniversalMenu extends DirtyChestMenu {
@Getter
private final UUID uuid;

public UniversalMenu(@Nonnull UniversalMenuPreset preset, @Nonnull UUID uuid) {
this(preset, uuid, (Location) null);
}

public UniversalMenu(@Nonnull UniversalMenuPreset preset, @Nonnull UUID uuid, Location lastPresent) {
super(preset);
this.uuid = uuid;

preset.clone(this, lastPresent);
this.getContents();
}

public UniversalMenu(
@Nonnull UniversalMenuPreset preset, @Nonnull UUID uuid, Location lastPresent, ItemStack[] contents) {
super(preset);
this.uuid = uuid;

for (int i = 0; i < contents.length; i++) {
var item = contents[i];
if (item == null || item.getType().isAir()) {
continue;
}
addItem(i, item);
}

preset.clone(this, lastPresent);
this.getContents();
}

public UniversalMenu(@Nonnull UniversalMenuPreset preset, @Nonnull UUID uuid, ItemStack[] contents) {
this(preset, uuid, null, contents);
}

public void update(@Nonnull Location lastPresent) {
((UniversalMenuPreset) preset).clone(this, lastPresent);
}

/**
* This method drops the contents of this {@link BlockMenu} on the ground at the given
* {@link Location}.
*
* @param l Where to drop these items
* @param slots The slots of items that should be dropped
*/
public void dropItems(Location l, int... slots) {
for (int slot : slots) {
ItemStack item = getItemInSlot(slot);

if (item != null) {
l.getWorld().dropItemNaturally(l, item);
replaceExistingItem(slot, null);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package city.norain.slimefun4.api.menu;

import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu;
import org.bukkit.Location;
import org.bukkit.block.Block;

public abstract class UniversalMenuPreset extends BlockMenuPreset {
/**
* Creates a new ChestMenu with the specified
* Title
*
* @param title The title of the Menu
*/
public UniversalMenuPreset(@Nonnull String id, @Nonnull String title) {
super(id, title);
}

public void newInstance(@Nonnull UniversalMenu menu, @Nonnull Block b) {
// This method can optionally be overridden by implementations
}

@Override
protected void clone(@Nonnull DirtyChestMenu menu) {
if (menu instanceof UniversalMenu universalMenu) {
var uniData = StorageCacheUtils.getUniversalBlock(universalMenu.getUuid());

if (uniData == null) {
return;
}

clone(universalMenu, uniData.getLastPresent().toLocation());
}
}

protected void clone(@Nonnull UniversalMenu menu, @Nonnull Location lastPresent) {
menu.setPlayerInventoryClickable(true);

for (int slot : occupiedSlots) {
menu.addItem(slot, getItemInSlot(slot));
}

if (getSize() > -1) {
menu.addItem(getSize() - 1, null);
}

newInstance(menu, lastPresent.getBlock());

for (int slot = 0; slot < 54; slot++) {
if (getMenuClickHandler(slot) != null) {
menu.addMenuClickHandler(slot, getMenuClickHandler(slot));
}
}

menu.addMenuOpeningHandler(getMenuOpeningHandler());
menu.addMenuCloseHandler(getMenuCloseHandler());
}

@Nullable public static UniversalMenuPreset getPreset(@Nullable String id) {
if (id == null) {
return null;
} else {
var preset = Slimefun.getRegistry().getMenuPresets().get(id);
if (preset instanceof UniversalMenuPreset uniPreset) {
return uniPreset;
} else {
return null;
}
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/city/norain/slimefun4/utils/ClassUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package city.norain.slimefun4.utils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class ClassUtil {
public String getCallerClass() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

if (stackTrace.length > 3) {
return stackTrace[3].getClassName();
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlCommonAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlUtils;
Expand All @@ -39,6 +41,7 @@ public void initStorage(DataType type) {
blockDataTable = SqlUtils.mapTable(DataScope.BLOCK_DATA, config.tablePrefix());
blockInvTable = SqlUtils.mapTable(DataScope.BLOCK_INVENTORY, config.tablePrefix());
chunkDataTable = SqlUtils.mapTable(DataScope.CHUNK_DATA, config.tablePrefix());
universalInvTable = SqlUtils.mapTable(DataScope.UNIVERSAL_INVENTORY, config.tablePrefix());
createBlockStorageTables();
}
}
Expand Down Expand Up @@ -124,6 +127,9 @@ private void createBlockStorageTables() {
createBlockDataTable();
createBlockInvTable();
createChunkDataTable();
createUniversalInventoryTable();
createUniversalRecordTable();
createUniversalDataTable();
}

private void createProfileTable() {
Expand Down Expand Up @@ -312,4 +318,65 @@ private void createBlockInvTable() {
+ ")"
+ ");");
}

private void createUniversalInventoryTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ universalInvTable
+ "("
+ FIELD_UNIVERSAL_UUID
+ " CHAR(64) NOT NULL, "
+ FIELD_INVENTORY_SLOT
+ " TINYINT UNSIGNED NOT NULL, "
+ FIELD_INVENTORY_ITEM
+ " CHAR(64) NOT NULL,"
+ "PRIMARY KEY ("
+ FIELD_UNIVERSAL_UUID
+ ", "
+ FIELD_INVENTORY_SLOT
+ ")"
+ ");");
}

private void createUniversalRecordTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ universalRecordTable
+ "("
+ FIELD_UNIVERSAL_UUID
+ " CHAR(64) NOT NULL, "
+ FIELD_SLIMEFUN_ID
+ " CHAR(64) NOT NULL, "
+ FIELD_UNIVERSAL_TRAITS
+ " CHAR(64) NOT NULL, "
+ "PRIMARY KEY ("
+ FIELD_UNIVERSAL_UUID
+ ")"
+ ");");
}

private void createUniversalDataTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ universalDataTable
+ "("
+ FIELD_UNIVERSAL_UUID
+ " CHAR(64) NOT NULL, "
+ FIELD_DATA_KEY
+ " CHAR(64) NOT NULL, "
+ FIELD_DATA_VALUE
+ " TEXT NOT NULL, "
+ "FOREIGN KEY ("
+ FIELD_UNIVERSAL_UUID
+ ") "
+ "REFERENCES "
+ universalRecordTable
+ "("
+ FIELD_UNIVERSAL_UUID
+ ") "
+ "ON UPDATE CASCADE ON DELETE CASCADE, "
+ "PRIMARY KEY ("
+ FIELD_UNIVERSAL_UUID
+ ", "
+ FIELD_DATA_KEY
+ ")"
+ ");");
}
}
Loading

0 comments on commit 091919e

Please sign in to comment.