Skip to content

Commit 22b3396

Browse files
committed
Add nexo support
1 parent 2437cbb commit 22b3396

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

bukkit/src/main/java/com/clubobsidian/dynamicgui/bukkit/plugin/DynamicGuiBukkitPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.clubobsidian.dynamicgui.bukkit.permission.VaultPermission;
3535
import com.clubobsidian.dynamicgui.bukkit.platform.BukkitPlatform;
3636
import com.clubobsidian.dynamicgui.bukkit.registry.model.ItemsAdderModelProvider;
37+
import com.clubobsidian.dynamicgui.bukkit.registry.model.NexoModelProvider;
3738
import com.clubobsidian.dynamicgui.bukkit.registry.model.OraxenModelProvider;
3839
import com.clubobsidian.dynamicgui.bukkit.registry.npc.CitizensRegistry;
3940
import com.clubobsidian.dynamicgui.bukkit.registry.replacer.PlaceholderApiReplacerRegistry;
@@ -161,6 +162,9 @@ private void registerModelProviders(PluginManager pm) {
161162
if (pm.getPlugin("ItemsAdder") != null) {
162163
ModelManager.get().register(new ItemsAdderModelProvider());
163164
}
165+
if (pm.getPlugin("Nexo") != null) {
166+
ModelManager.get().register(new NexoModelProvider());
167+
}
164168
}
165169

166170
@Override
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2018-2023 virustotalop
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.clubobsidian.dynamicgui.bukkit.registry.model;
18+
19+
import com.clubobsidian.dynamicgui.api.DynamicGui;
20+
import com.clubobsidian.dynamicgui.api.inventory.ItemStackWrapper;
21+
import com.clubobsidian.dynamicgui.api.manager.inventory.ItemStackManager;
22+
import com.clubobsidian.dynamicgui.api.model.ModelProvider;
23+
import com.clubobsidian.dynamicgui.core.util.ReflectionUtil;
24+
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
27+
28+
public class NexoModelProvider implements ModelProvider {
29+
30+
private static final boolean NEXO_EXISTS = ReflectionUtil.classExists("com.nexomc.nexo.api.NexoItems");
31+
private static final Method GET_ITEM_BUILDER = itemBuilder();
32+
private static final Method GET_ITEM_STACK = itemStack();
33+
34+
private static Method itemBuilder() {
35+
if (NEXO_EXISTS) {
36+
try {
37+
Class<?> customStack = Class.forName("com.nexomc.nexo.api.NexoItems");
38+
Method getInstance = customStack.getDeclaredMethod("itemFromId", String.class);
39+
getInstance.setAccessible(true);
40+
return getInstance;
41+
} catch (ClassNotFoundException | NoSuchMethodException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
return null;
46+
}
47+
48+
private static Method itemStack() {
49+
if (NEXO_EXISTS) {
50+
try {
51+
Class<?> customStack = Class.forName("com.nexomc.nexo.items.ItemBuilder");
52+
Method build = customStack.getDeclaredMethod("build");
53+
build.setAccessible(true);
54+
return build;
55+
} catch (ClassNotFoundException | NoSuchMethodException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
return null;
60+
}
61+
62+
@Override
63+
public String name() {
64+
return "nexo";
65+
}
66+
67+
@Override
68+
public boolean applyModel(ItemStackWrapper<?> itemStack, String data) {
69+
if (NEXO_EXISTS) {
70+
try {
71+
Object itemBuilder = GET_ITEM_BUILDER.invoke(null, data);
72+
if (itemBuilder != null) {
73+
ItemStackWrapper<?> wrappedCustom = ItemStackManager
74+
.get()
75+
.createItemStackWrapper(GET_ITEM_STACK.invoke(itemBuilder));
76+
if (wrappedCustom.hasCustomModel()) {
77+
return itemStack.setModel(wrappedCustom.getModelData());
78+
} else {
79+
DynamicGui.get().getLogger().error("Cannot find model data for %s", data);
80+
}
81+
} else {
82+
DynamicGui.get().getLogger().error("No model data found '%s' for provider '%s'",
83+
data,
84+
this.name()
85+
);
86+
}
87+
} catch (IllegalAccessException | InvocationTargetException e) {
88+
e.printStackTrace();
89+
}
90+
} else {
91+
DynamicGui.get().getLogger().error("Failed applying ItemsAdder model due to an outdated API");
92+
}
93+
return false;
94+
}
95+
}

0 commit comments

Comments
 (0)