Skip to content

Commit

Permalink
* ContainerModification now inherits ContainerQuestion
Browse files Browse the repository at this point in the history
* Fixed hard stop user validation issues on start up
* Created new MenuRegistration service
* Created new menu type Animated
* Fast deprecated PlayerSearch#getRecordedId with PlayerSearch#getId
* Added playtime variable to PlayerSearch (beta)
* Removed redundant Cooldown#getTimeLeft method
* Item.java can now be used to set empty lore.
* SimpleTabCompletion can have multiple completions mapped to the same index.
* Fixed new player spawn location problem with region service impl
* Added methods to StringUtils
* Fixed nms problems with chat component itemstack content in 1.18
  • Loading branch information
Hempfest committed Jan 6, 2022
1 parent b746b03 commit f1c4d17
Show file tree
Hide file tree
Showing 34 changed files with 542 additions and 180 deletions.
2 changes: 1 addition & 1 deletion labyrinth-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>labyrinth</artifactId>
<groupId>com.github.the-h-team</groupId>
<version>1.7.4</version>
<version>1.7.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,7 @@
import com.github.sanctum.labyrinth.annotation.Note;
import com.github.sanctum.labyrinth.api.LabyrinthAPI;
import com.github.sanctum.labyrinth.api.Service;
import com.github.sanctum.labyrinth.data.LabyrinthPluginMessageEvent;
import com.github.sanctum.labyrinth.data.LabyrinthUser;
import com.github.sanctum.labyrinth.data.LabyrinthPluginChannel;
import com.github.sanctum.labyrinth.data.LabyrinthPluginMessage;
import com.github.sanctum.labyrinth.data.ServiceType;
import com.github.sanctum.labyrinth.data.service.Constant;
import com.github.sanctum.labyrinth.event.custom.Vent;
import com.github.sanctum.labyrinth.library.Deployable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

/**
* ▄▄▌***▄▄▄·*▄▄▄▄·**▄·*▄▌▄▄▄**▪***▐*▄*▄▄▄▄▄*▄*.▄
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public class LabyrinthUser implements Nameable {
public LabyrinthUser(String name) {
this.name = name;
PlayerSearch lookup = PlayerSearch.of(name);
this.id = lookup.getRecordedId();
this.id = lookup.getId();
this.target = lookup.getPlayer();
}

public LabyrinthUser(OfflinePlayer pl) {
this.name = pl.getName();
PlayerSearch lookup = PlayerSearch.of(pl);
this.target = lookup.getPlayer();
this.id = lookup.getRecordedId();
this.id = lookup.getId();
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sanctum.labyrinth.data.service;

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.annotation.Note;
import com.github.sanctum.labyrinth.data.FileList;
import com.github.sanctum.labyrinth.data.FileManager;
import com.github.sanctum.labyrinth.data.FileType;
Expand All @@ -11,6 +12,7 @@
import com.github.sanctum.labyrinth.formatting.string.SpecialID;
import com.github.sanctum.labyrinth.interfacing.Nameable;
import com.github.sanctum.labyrinth.library.Deployable;
import com.github.sanctum.labyrinth.library.TimeWatch;
import java.util.Arrays;
import java.util.UUID;
import org.bukkit.Bukkit;
Expand All @@ -23,34 +25,43 @@
*/
public abstract class PlayerSearch implements Nameable {

static final Plugin access = LabyrinthProvider.getInstance().getPluginInstance();
static final FileManager users = FileList.search(access).get("users", "Persistent", FileType.JSON);
static final LabyrinthMap<String, PlayerSearch> lookups = new LabyrinthEntryMap<>();

@Override
public abstract @NotNull String getName();

public abstract @NotNull OfflinePlayer getPlayer();

@Deprecated
public abstract @NotNull UUID getRecordedId();

public final UUID getId() {
return getRecordedId();
}

public abstract @NotNull SpecialID getSpecialId();

public final TimeWatch.Recording getPlaytime() {
return TimeWatch.Recording.subtract(getPlayer().getFirstPlayed());
}

public static PlayerSearch of(@NotNull OfflinePlayer player) {
return lookups.computeIfAbsent(player.getName(), s -> new PlayerSearch() {
String name = player.getName();
if (name == null) {
LabyrinthProvider.getInstance().getLogger().severe("- Attempted and failed to register corrupt player data (" + player + "), this is not the fault of labyrinth.");
return null;
}
return lookups.computeIfAbsent(name, s -> new PlayerSearch() {

final OfflinePlayer parent;
final String name;
final UUID reference;
final boolean online;
{
this.parent = validate(player);
this.name = validate(player.getName());
if (users.read(c -> c.isNode(name))) {
reference = users.read(c -> UUID.fromString(c.getNode(name).getNode("id").toPrimitive().getString()));
} else {
reference = player.getUniqueId();
users.write(t -> t.set(name + ".id", reference.toString()));
}
this.online = Bukkit.getOnlineMode();
this.name = s;
reference = player.getUniqueId();
}

<T> T validate(T t) {
Expand All @@ -65,15 +76,16 @@ <T> T validate(T t) {

@Override
public @NotNull OfflinePlayer getPlayer() {
return parent;
return online ? parent : Bukkit.getOfflinePlayer(getName());
}

@Override
public @NotNull UUID getRecordedId() {
return reference;
return online ? reference : getPlayer().getUniqueId();
}

@Override
@Note("Length of 12 (HUID)")
public @NotNull SpecialID getSpecialId() {
return SpecialID.builder().setLength(12).build(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import com.github.sanctum.labyrinth.LabyrinthProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.github.sanctum.labyrinth.formatting;

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.annotation.Experimental;
import com.github.sanctum.labyrinth.formatting.component.ActionComponent;
import com.github.sanctum.labyrinth.formatting.string.CustomColor;
import com.github.sanctum.labyrinth.library.Applicable;
import com.github.sanctum.labyrinth.library.HUID;
import com.github.sanctum.labyrinth.library.ListUtils;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.TaskScheduler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -207,90 +208,70 @@ public ToolTip<String> color(Color color) {

public static class Item extends ToolTip<ItemStack> {

private final ItemStack message;
private final String json;

public Item(ItemStack message) {
this.message = message;
this.json = convertItemStackToJson(message);
}

private Class<?> getItemClass() {
private final @Experimental Supplier<Class<?>> ITEMSTACK_NMS = () -> {
String name = Bukkit.getServer().getClass().getPackage().getName();
String version = name.substring(name.lastIndexOf('.') + 1) + ".";
String clazzName = "org.bukkit.craftbukkit." + version + "inventory.CraftItemStack";
Class<?> clazz;

String clazzName = "org.bukkit.craftbukkit." + (name.substring(name.lastIndexOf('.') + 1) + ".") + "inventory.CraftItemStack";
try {
clazz = Class.forName(clazzName);
return Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
return null;
}
return clazz;
}
};
private final Supplier<Method> NMS_COPY = () -> getMethod(ITEMSTACK_NMS.get(), "asNMSCopy", ItemStack.class);

private final ItemStack message;
private final String json;

public Item(ItemStack message) {
this.message = message;
this.json = itemToJson(message);
}

private Class<?> getNMSClass(String nmsClassName) {
private Class<?> getNMSClass(String nmsClass) {
String name = Bukkit.getServer().getClass().getPackage().getName();
String version = name.substring(name.lastIndexOf('.') + 1) + ".";
String clazzName = "net.minecraft.server." + version + nmsClassName;
Class<?> clazz;
String clazzName = "net.minecraft.server." + version + nmsClass;
try {
clazz = Class.forName(clazzName);
return Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
LabyrinthProvider.getInstance().getLogger().severe("- You should never see this message, class '" + nmsClass + "' not found.");
return null;
}
return clazz;
}

private Class<?> getNewClass(String nmsClassName) {
Class<?> clazz;
private Class<?> getNewClass(String nonNMSClass) {
try {
clazz = Class.forName(nmsClassName);
return Class.forName(nonNMSClass);
} catch (Throwable t) {
t.printStackTrace();
LabyrinthProvider.getInstance().getLogger().severe("- You should never see this message, class '" + nonNMSClass + "' not found.");
return null;
}
return clazz;
}

private Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
try {
return clazz.getMethod(methodName, params);
} catch (Exception e) {
e.printStackTrace();
} catch (Exception ignored) {
LabyrinthProvider.getInstance().getLogger().severe("- Method with name '" + methodName + "' doesn't exist on runtime.");
return null;
}
}

private String convertItemStackToJson(ItemStack itemStack) {
// ItemStack methods to get a net.minecraft.server.ItemStack object for serialization
Class<?> craftItemStackClazz = getItemClass();
Method asNMSCopyMethod = getMethod(craftItemStackClazz, "asNMSCopy", ItemStack.class);

// NMS Method to serialize a net.minecraft.server.ItemStack to a valid Json string
Class<?> nmsItemStackClazz = Bukkit.getVersion().contains("1.17") ? getNewClass("net.minecraft.world.item.ItemStack") : getNMSClass("ItemStack");
Class<?> nbtTagCompoundClazz = Bukkit.getVersion().contains("1.17") ? getNewClass("net.minecraft.nbt.NBTTagCompound") : getNMSClass("NBTTagCompound");
Method saveNmsItemStackMethod = getMethod(nmsItemStackClazz, "save", nbtTagCompoundClazz);

Object nmsNbtTagCompoundObj; // This will just be an empty NBTTagCompound instance to invoke the saveNms method
Object nmsItemStackObj; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method
Object itemAsJsonObject; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method

private String itemToJson(ItemStack itemStack) {
boolean isBrandNew = Bukkit.getVersion().contains("1.18");
boolean isNew = Bukkit.getVersion().contains("1.17") || isBrandNew;
Class<?> itemStackClass = isNew ? getNewClass("net.minecraft.world.item.ItemStack") : getNMSClass("ItemStack");
Class<?> nbtTagCompoundClass = isNew ? getNewClass("net.minecraft.nbt.NBTTagCompound") : getNMSClass("NBTTagCompound");
// on runtime some 1.18 bukkit source is still obfuscated for some reason, so we have to use it's obfuscated name 'b'
Method saveNBT = getMethod(itemStackClass, isBrandNew ? "b" : "save", nbtTagCompoundClass);
if (saveNBT == null || nbtTagCompoundClass == null) return null;
try {
nmsNbtTagCompoundObj = nbtTagCompoundClazz.getDeclaredConstructor().newInstance();
nmsItemStackObj = asNMSCopyMethod.invoke(null, itemStack);
itemAsJsonObject = saveNmsItemStackMethod.invoke(nmsItemStackObj, nmsNbtTagCompoundObj);
return saveNBT.invoke(NMS_COPY.get().invoke(null, itemStack), nbtTagCompoundClass.getDeclaredConstructor().newInstance()).toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}

// Return a string representation of the serialized object
return itemAsJsonObject.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SimpleTabCompletion then(int index, Collection<String> completions) {
if (non_predicates.containsKey(index)) {
non_predicates.get(index).addAll(completions);
} else {
non_predicates.put(index, completions);
non_predicates.put(index, new ArrayList<>(completions));
}
return this;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public SimpleTabCompletion then(int index, Collection<String> completions) {
map.get(index).addAll(completions);
}
} else {
map.put(index, completions);
map.put(index, new ArrayList<>(completions));
}
return this;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public String setValue(String value) {
map.get(index).addAll(completions);
}
} else {
map.put(index, completions);
map.put(index, new ArrayList<>(completions));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.github.sanctum.labyrinth.library;

import org.bukkit.Material;
import org.bukkit.block.Container;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public final class ContainerModification<T extends Container> {
public final class ContainerModification<T extends Container> extends ContainerQuestion<T> {

final ContainerQuery<T> query;
final Inventory snapshot;

ContainerModification(ContainerQuery<T> query) {
super(query);
this.query = query;
this.snapshot = query.container.getSnapshotInventory();
}
Expand Down Expand Up @@ -46,5 +48,23 @@ public ContainerQuery<T> update() {
return query;
}

public boolean has(@NotNull ItemStack itemStack) {
return snapshot.contains(itemStack);
}

public boolean has(@NotNull Material material) {
return snapshot.contains(material);
}

public boolean has(@NotNull ItemStack itemStack, int amount) {
return snapshot.contains(itemStack, amount);
}

public boolean has(@NotNull Material material, int amount) {
return snapshot.contains(material, amount);
}

public boolean isEmpty() {
return snapshot.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public final class ContainerQuestion<T extends Container> {
public class ContainerQuestion<T extends Container> {

final ContainerQuery<T> queue;

Expand Down

0 comments on commit f1c4d17

Please sign in to comment.