forked from SlimefunGuguProject/Slimefun4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
75 changed files
with
2,754 additions
and
1,223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/city/norain/slimefun4/api/menu/UniversalMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/city/norain/slimefun4/api/menu/UniversalMenuPreset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.