Skip to content

Commit

Permalink
fix(unidata): more fix on universal menu
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Aug 7, 2024
1 parent 291360a commit e3141e5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,20 @@ public SlimefunBlockData getBlockDataFromCache(Location l) {
@Nullable public SlimefunUniversalData getUniversalData(@Nonnull UUID uuid) {
checkDestroy();

if (loadedUniversalData.containsKey(uuid) && loadedUniversalData.get(uuid) != null) {
return loadedUniversalData.get(uuid);
}

var key = new RecordKey(DataScope.UNIVERSAL_RECORD);
key.addCondition(FieldKey.UNIVERSAL_UUID, uuid.toString());
key.addField(FieldKey.SLIMEFUN_ID);
key.addField(FieldKey.LAST_PRESENT);

var result = getData(key);

Slimefun.logger().log(Level.INFO, "Got data {0}", result.stream().map(set -> set.getAll()
.toString()));

return result.isEmpty()
? null
: new SlimefunUniversalData(
Expand Down Expand Up @@ -650,6 +657,8 @@ public void loadUniversalData() {
var cache = getUniversalDataFromCache(uuid);
var uniData = cache == null ? new SlimefunUniversalData(uuid, location, sfId) : cache;

Slimefun.logger().log(Level.INFO, "Loaded universal data {0}", uuid);

if (sfItem.loadDataByDefault()) {
scheduleReadTask(() -> loadUniversalData(uniData));
}
Expand Down Expand Up @@ -714,6 +723,7 @@ public void loadBlockData(SlimefunBlockData blockData) {
getData(menuKey)
.forEach(record -> inv[record.getInt(FieldKey.INVENTORY_SLOT)] =
record.getItemStack(FieldKey.INVENTORY_ITEM));

blockData.setBlockMenu(new BlockMenu(menuPreset, blockData.getLocation(), inv));

var content = blockData.getMenuContents();
Expand Down Expand Up @@ -783,9 +793,7 @@ public void loadUniversalData(SlimefunUniversalData uniData) {
.forEach(recordSet -> inv[recordSet.getInt(FieldKey.INVENTORY_SLOT)] =
recordSet.getItemStack(FieldKey.INVENTORY_ITEM));

var universalMenu = new UniversalMenu(menuPreset, uniData.getUUID(), inv);

uniData.setUniversalMenu(universalMenu);
uniData.setUniversalMenu(new UniversalMenu(menuPreset, uniData, inv));

var content = uniData.getMenuContents();
if (content != null) {
Expand All @@ -796,12 +804,12 @@ public void loadUniversalData(SlimefunUniversalData uniData) {
var sfItem = SlimefunItem.getById(uniData.getSfId());

if (sfItem != null && sfItem.isTicking()) {
loadedUniversalData.putIfAbsent(uniData.getUUID(), uniData);

if (!uniData.getLastPresent().getBlock().getType().isAir()) {
Slimefun.getTickerTask().enableTicker(uniData.getUUID(), uniData.getLastPresent());
}
}

loadedUniversalData.putIfAbsent(uniData.getUUID(), uniData);
} finally {
lock.unlock(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class StorageCacheUtils {

@ParametersAreNonnullByDefault
public static boolean hasBlock(Location l) {
return getBlock(l) != null;
return getBlock(l) != null || getUniversalData(l.getBlock()) != null;
}

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -64,7 +64,18 @@ public static boolean isBlock(Location l, String id) {
@ParametersAreNonnullByDefault
@Nullable public static String getData(Location loc, String key) {
var blockData = getBlock(loc);
return blockData == null ? null : blockData.getData(key);

if (blockData != null) {
return blockData.getData(key);
} else {
var uniData = getUniversalData(loc.getBlock());

if (uniData == null) {
return null;
}

return uniData.getData(key);
}
}

@ParametersAreNonnullByDefault
Expand All @@ -76,14 +87,25 @@ public static boolean isBlock(Location l, String id) {
@ParametersAreNonnullByDefault
public static void setData(Location loc, String key, String val) {
var block = getBlock(loc);
Preconditions.checkNotNull(block);

block.setData(key, val);
if (block != null) {
block.setData(key, val);
} else {
var uni = getUniversalData(loc.getBlock());
Preconditions.checkNotNull(uni);
uni.setData(key, val);
}
}

@ParametersAreNonnullByDefault
public static void removeData(Location loc, String key) {
getBlock(loc).removeData(key);
var block = getBlock(loc);
if (block != null) {
block.removeData(key);
} else {
var uni = getUniversalData(loc.getBlock());
Preconditions.checkNotNull(uni);
uni.removeData(key);
}
}

@ParametersAreNonnullByDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> dro
}

var uniData =
Slimefun.getDatabaseManager().getBlockDataController().getUniversalData(uuid.get());
Slimefun.getDatabaseManager().getBlockDataController().getUniversalDataFromCache(uuid.get());

if (uniData != null) {
if (!e.getPlayer().hasPermission("slimefun.android.bypass")
Expand Down Expand Up @@ -1037,7 +1037,7 @@ protected void move(Block from, BlockFace face, Block to) {
+ LocationUtils.locationToString(from.getLocation()));
}

OfflinePlayer owner = Bukkit.getOfflinePlayer(uniData.getData("owner"));
OfflinePlayer owner = Bukkit.getOfflinePlayer(UUID.fromString(uniData.getData("owner")));

if (!Slimefun.getProtectionManager().hasPermission(owner, to.getLocation(), Interaction.PLACE_BLOCK)) {
return;
Expand All @@ -1051,6 +1051,8 @@ protected void move(Block from, BlockFace face, Block to) {
return;
}

Slimefun.getTickerTask().disableTicker(uniData.getUUID());

to.setBlockData(Material.PLAYER_HEAD.createBlockData(data -> {
if (data instanceof Rotatable rotatable) {
rotatable.setRotation(face.getOppositeFace());
Expand All @@ -1068,6 +1070,9 @@ protected void move(Block from, BlockFace face, Block to) {

from.setType(Material.AIR);
uniData.setLastPresent(to.getLocation());
uniData.getUniversalMenu().update();

Slimefun.getTickerTask().enableTicker(uniData.getUUID(), to.getLocation());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ private void sendInfo(Player p, Block b, ASlimefunDataContainer data) {
p.sendMessage(ChatColors.color("&dInventory: " + redCross));
}

if (data instanceof SlimefunUniversalData) {
if (data instanceof SlimefunUniversalData universalData) {
p.sendMessage(ChatColors.color("&dUniversal Item: " + greenCheckmark));
p.sendMessage(ChatColors.color(" &dUUID: " + universalData.getUUID()));
}

if (item.isTicking()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void tickLocation(@Nonnull Set<BlockTicker> tickers, @Nonnull Location l

@ParametersAreNonnullByDefault
private void tickUniversalLocation(UUID uuid, Location l, @Nonnull Set<BlockTicker> tickers) {
var data = StorageCacheUtils.getUniversalData(uuid, l);
var data = StorageCacheUtils.getUniversalData(uuid);
var item = SlimefunItem.getById(data.getSfId());

if (item != null && item.getBlockTicker() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.mrCookieSlime.Slimefun.api.inventory;

import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunUniversalData;
import java.util.UUID;
import javax.annotation.Nonnull;
import lombok.Getter;
Expand Down Expand Up @@ -38,6 +39,27 @@ public UniversalMenu(@Nonnull UniversalMenuPreset preset, @Nonnull UUID uuid, It
this.getContents();
}

public UniversalMenu(
@Nonnull UniversalMenuPreset preset, @Nonnull SlimefunUniversalData universalData, ItemStack[] contents) {
super(preset);
this.uuid = universalData.getUUID();

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

preset.clone(this, universalData.getLastPresent());
this.getContents();
}

public void update() {
preset.clone(this);
}

/**
* This method drops the contents of this {@link BlockMenu} on the ground at the given
* {@link Location}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.Location;
import org.bukkit.block.Block;

public abstract class UniversalMenuPreset extends BlockMenuPreset {
Expand All @@ -30,31 +31,31 @@ protected void clone(@Nonnull DirtyChestMenu menu) {
return;
}

if (!uniData.isDataLoaded()) {
StorageCacheUtils.requestLoad(uniData);
}
clone(universalMenu, uniData.getLastPresent());
}
}

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

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

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

newInstance(universalMenu, uniData.getLastPresent().getBlock());
newInstance(menu, lastPresent.getBlock());

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

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

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

@Nullable public static UniversalMenuPreset getPreset(@Nullable String id) {
Expand Down

0 comments on commit e3141e5

Please sign in to comment.