Skip to content

Commit

Permalink
Merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Nov 9, 2024
2 parents d50872a + 091919e commit 5614548
Show file tree
Hide file tree
Showing 75 changed files with 2,754 additions and 1,223 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,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.mcchampions.dough</groupId>
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 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.
*/
@Getter
public class UniversalMenu extends DirtyChestMenu {
private final UUID uuid;

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

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

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

public UniversalMenu(
UniversalMenuPreset preset, 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(UniversalMenuPreset preset, UUID uuid, ItemStack[] contents) {
this(preset, uuid, null, contents);
}

public void update(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,77 @@
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.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(String id, String title) {
super(id, title);
}

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

@Override
protected void clone(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(UniversalMenu menu, 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;
}
}
}
Loading

0 comments on commit 5614548

Please sign in to comment.