Skip to content

Commit b0eae7a

Browse files
committed
[Optimization] Updated ItemDisplay - unified withing VisualTask (no more tasks-per-display) and adjusted the degrees to match with the VisualTask speed, cleaned up code little bit more
Signed-off-by: petulikan1 <petulikan@gmail.com> Took 1 hour 26 minutes
1 parent 932fa66 commit b0eae7a

File tree

6 files changed

+64
-55
lines changed

6 files changed

+64
-55
lines changed

Core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>Core</artifactId>
13-
<version>2.0.1</version>
13+
<version>2.0.2</version>
1414

1515
<properties>
1616
<maven.compiler.source>8</maven.compiler.source>

Core/src/main/java/xzot1k/plugins/ds/DisplayShops.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ public void onEnable() {
136136
log(Level.WARNING,"Server is not compatible with ItemDisplays! Only 1.19.4+ and Paper");
137137
}
138138

139-
if (getPluginInstance().getDisplayManager() != null) {
139+
fixConfig();
140+
141+
if (getDisplayManager() != null) {
140142
Display.ClearAllEntities();
141143
}
142144

143-
fixConfig();
144-
145145
menuMap = new HashMap<>();
146146
shopMemory = new HashMap<>();
147147
displayPacketMap = new HashMap<>();
@@ -158,7 +158,7 @@ public void onEnable() {
158158
throw new RuntimeException("Plugin is not compatible with this server version! Your server version: "+Ref.serverVersion());
159159
}
160160

161-
if (getDisplayManager() != null) {Display.ClearAllEntities();}
161+
162162

163163
setPaperSpigot(false);
164164
Method[] methods = World.class.getMethods();
@@ -240,7 +240,7 @@ public void onEnable() {
240240
getServer().getScheduler().runTask(this, () -> {
241241
getManager().loadShops(false, false);
242242
getManager().loadMarketRegions(false);
243-
getServer().getOnlinePlayers().parallelStream().forEach(player -> getManager().loadDataPack(player));
243+
getServer().getOnlinePlayers().forEach(player -> getManager().loadDataPack(player));
244244
});
245245

246246
setupTasks();
@@ -592,10 +592,10 @@ public void setupRecipe() {
592592
if (getConfig().getBoolean("shop-creation-item.craftable")) {
593593
try {
594594
ShapedRecipe shapedRecipe;
595-
if (Ref.serverVersionInt()>=9) {
595+
if (Ref.isNewerThan(8)) {
596596
org.bukkit.NamespacedKey namespacedKey = new org.bukkit.NamespacedKey(this, "shop");
597597

598-
if (Ref.serverVersionInt()>=16) {
598+
if (Ref.isNewerThan(15)) {
599599
Recipe recipe = getServer().getRecipe(namespacedKey);
600600
if (recipe != null) getServer().removeRecipe(namespacedKey);
601601
}
@@ -800,9 +800,7 @@ public void setupTasks() {
800800
}
801801

802802
setInSightTask(new VisualTask(this));
803-
//if (getDisplayManager() != null) {getInSightTask().runTaskTimer(this, 1, getConfig().getInt("view-tick"));} else {
804-
getInSightTask().runTaskTimerAsynchronously(this, 60, 4/*getConfig().getInt("view-tick")*/);
805-
// }
803+
getInSightTask().runTaskTimerAsynchronously(this, 60, 4);
806804

807805
setVisitItemTask(new VisitItemTask(this));
808806
getVisitItemTask().runTaskTimerAsynchronously(this, 20, (20 * 5));
@@ -1018,7 +1016,7 @@ public void fixConfig() {
10181016
else if (value.equalsIgnoreCase("ENTITY_ENDERMAN_TELEPORT")) immersionSection.set(entry.getKey(), "ENDERMAN_TELEPORT");
10191017
else if (value.equalsIgnoreCase("ENTITY_SNOWBALL_THROW")) immersionSection.set(entry.getKey(), "SHOOT_ARROW");
10201018
}
1021-
} else if (Ref.serverVersionInt() >= 9) {
1019+
} else if (Ref.isNewerThan(8)) {
10221020
ConfigurationSection immersionSection = getConfig().getConfigurationSection("immersion-section");
10231021
if (immersionSection != null) for (Map.Entry<String, Object> entry : immersionSection.getValues(true).entrySet()) {
10241022
final String value = String.valueOf(entry.getValue());

Core/src/main/java/xzot1k/plugins/ds/api/DManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public String getValueFromPlaceholder(@NotNull ItemStack itemStack, @NotNull Obj
487487
public String getItemName(@NotNull ItemStack itemStack) {
488488
if (itemStack.hasItemMeta() && itemStack.getItemMeta() != null && itemStack.getItemMeta().hasDisplayName())
489489
return itemStack.getItemMeta().getDisplayName();//.replace("\"", "\\\"").replace("'", "\\'");
490-
else return Ref.serverVersionInt()>=13 ? getPluginInstance().getManager().getTranslatedName(itemStack.getType(), itemStack.getDurability())
490+
else return Ref.isNewerThan(12) ? getPluginInstance().getManager().getTranslatedName(itemStack.getType(), itemStack.getDurability())
491491
: getPluginInstance().getManager().getTranslatedName(itemStack.getType());
492492
//.replace("\"", "\\\"").replace("'", "\\'");
493493
}
@@ -533,7 +533,7 @@ public String getTranslatedName(Material material, int... data) {
533533
* @return The translated version.
534534
*/
535535
public String getTranslatedName(@NotNull Enchantment enchantment) {
536-
final boolean isNew = (Ref.serverVersionInt()>12);
536+
final boolean isNew = (Ref.isNewerThan(12));
537537
ConfigurationSection cs = getPluginInstance().getLangConfig().getConfigurationSection("translated-enchantment-names");
538538
if (cs != null) {
539539
Collection<String> keys = cs.getKeys(false);
@@ -672,7 +672,7 @@ public String getRomanNumeral(int number) {
672672
*/
673673
public String color(@NotNull String message) {
674674
if (message.isEmpty()) return message;
675-
if (Ref.serverVersionInt()>=16) {
675+
if (Ref.isNewerThan(15)) {
676676
Matcher matcher = hexPattern.matcher(message);
677677
while (matcher.find()) {
678678
final ChatColor hexColor = ChatColor.of(matcher.group());
@@ -1366,7 +1366,7 @@ public double getPromotionPriceModifier(Player player) {
13661366
public int getInventorySpaceForItem(@NotNull Player player, @NotNull ItemStack itemStack) {
13671367
int availableSpace = 0;
13681368

1369-
if (Ref.serverVersionInt()>=9) {
1369+
if (Ref.isNewerThan(8)) {
13701370
ItemStack[] contents = player.getInventory().getStorageContents();
13711371
for (int i = -1; ++i < contents.length; ) {
13721372
final ItemStack item = contents[i];

Core/src/main/java/xzot1k/plugins/ds/core/packets/Display.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.bukkit.inventory.ItemStack;
99
import org.bukkit.metadata.FixedMetadataValue;
1010
import org.bukkit.persistence.PersistentDataType;
11-
import org.bukkit.scheduler.BukkitRunnable;
1211
import org.jetbrains.annotations.NotNull;
1312
import org.joml.Matrix4f;
1413
import xzot1k.plugins.ds.DisplayShops;
@@ -158,13 +157,9 @@ private void updateItem(World world, Location baseLocation, float scale, double
158157
if (getEntityIds().contains(entity.getUniqueId())) {
159158
getEntityIds().add(entity.getUniqueId());
160159
}
160+
entity.setTransformationMatrix(new Matrix4f().scale(scale));
161161

162-
Matrix4f mat = new Matrix4f().scale(scale);
163-
entity.setTransformationMatrix(mat);
164162

165-
if (DisplayShops.getPluginInstance().getConfig().getBoolean("allow-item-spinning")) {
166-
rotateDisplay(entity, mat, scale, 5);
167-
}
168163
});
169164
} else {
170165
((ItemDisplay) getItemHolder()).setItemStack(item);
@@ -197,7 +192,7 @@ private void updateItem(World world, Location baseLocation, float scale, double
197192
getItemHolder().addPassenger(getItem()); // add item as passenger*/
198193
}
199194

200-
private void rotateDisplay(ItemDisplay display, Matrix4f mat, float scale, int duration) {
195+
/* private void rotateDisplay(ItemDisplay display, Matrix4f mat, float scale, int duration) {
201196
final float rotationIncrement = (float) Math.toRadians(5); // Rotate 5 degrees per tick
202197
final float[] currentAngle = {0}; // Array to hold current angle
203198
@@ -214,20 +209,18 @@ public void run() {
214209
currentAngle[0] -= (float) Math.toRadians(360); // Reset the angle if it completes a full rotation
215210
}
216211
217-
Matrix4f matrix = null;
218-
219212
ItemStack itemStack = display.getItemStack();
220213
if (itemStack != null) {
221214
if (itemStack.getType().name().contains("SHIELD")) {return;}
222215
}
223216
224217
// Update the transformation matrix with the new rotation
225-
display.setTransformationMatrix((matrix != null ? matrix : mat.identity()).scale(scale).rotateY(currentAngle[0]));
218+
display.setTransformationMatrix(mat.identity().scale(scale).rotateY(currentAngle[0]));
226219
display.setInterpolationDelay(0); // no delay to the interpolation
227220
display.setInterpolationDuration(duration); // set the duration of the interpolated rotation
228221
}
229-
}.runTaskTimer(DisplayShops.getPluginInstance(), 1, duration);
230-
}
222+
}.runTaskTimer(DisplayShops.getPluginInstance(), 15, duration);
223+
}*/
231224

232225
private void updateGlass(World world, Location baseLocation, double[] appearanceOffsets) {
233226
// return if glass is supposed to be hidden

Core/src/main/java/xzot1k/plugins/ds/core/tasks/VisualTask.java

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
package xzot1k.plugins.ds.core.tasks;
66

7+
import me.devtec.shared.Ref;
8+
import org.bukkit.Bukkit;
79
import org.bukkit.World;
810
import org.bukkit.entity.ItemDisplay;
911
import org.bukkit.entity.Player;
1012
import org.bukkit.inventory.ItemStack;
1113
import org.bukkit.scheduler.BukkitRunnable;
14+
import org.joml.Matrix4f;
1215
import xzot1k.plugins.ds.DisplayShops;
1316
import xzot1k.plugins.ds.api.objects.Appearance;
1417
import xzot1k.plugins.ds.api.objects.Shop;
@@ -21,18 +24,14 @@
2124
public class VisualTask extends BukkitRunnable {
2225

2326
private DisplayShops pluginInstance;
24-
private int //viewDistance,
25-
gcCounter;
2627
private boolean alwaysDisplay, pause = false;
2728
private LinkedList<UUID> shopsToRefresh, playersToRefresh;
2829

2930
public VisualTask(DisplayShops pluginInstance) {
3031
setPluginInstance(pluginInstance);
3132
setShopsToRefresh(new LinkedList<>());
3233
setPlayersToRefresh(new LinkedList<>());
33-
setGcCounter(0);
3434
setAlwaysDisplay(getPluginInstance().getConfig().getBoolean("always-display"));
35-
//setViewDistance(getPluginInstance().getConfig().getInt("view-distance"));
3635
}
3736

3837
@Override
@@ -102,8 +101,16 @@ public void run() {
102101

103102
float finalCurrentScale = currentScale;
104103
double finalX = x, finalY = y, finalZ = z;
105-
DisplayShops.getPluginInstance().getServer().getScheduler().runTask(DisplayShops.getPluginInstance(),
106-
() -> display.update(world, generateText, finalCurrentScale, finalX, finalY, finalZ, offsets));
104+
DisplayShops.getPluginInstance().getServer().getScheduler().runTask(DisplayShops.getPluginInstance(), () -> {
105+
display.update(world, generateText, finalCurrentScale, finalX, finalY, finalZ, offsets);
106+
});
107+
108+
if (DisplayShops.getPluginInstance().getConfig().getBoolean("allow-item-spinning")) {
109+
Matrix4f mat = new Matrix4f().scale(finalCurrentScale);
110+
rotateDisplay((ItemDisplay) Ref.get(display,"itemDisplay"), mat, finalCurrentScale, 5);
111+
}
112+
113+
107114

108115
for (Player player : getPluginInstance().getServer().getOnlinePlayers()) {
109116
if (player == null || !player.isOnline()) {continue;}
@@ -158,7 +165,8 @@ && getPluginInstance().getDisplayPacketMap().get(player.getUniqueId()).containsK
158165
player.getEyeLocation().toVector(), player.getEyeLocation().getDirection(), 10/*getViewDistance()*/);
159166

160167
if (foundShopAtLocation == null) {
161-
if (currentShop != null) getPluginInstance().sendDisplayPacket(currentShop, player, false);
168+
if (currentShop != null)
169+
getPluginInstance().sendDisplayPacket(currentShop, player, false);
162170
getPluginInstance().getShopMemory().remove(player.getUniqueId());
163171
continue;
164172
}
@@ -174,11 +182,36 @@ && getPluginInstance().getDisplayPacketMap().get(player.getUniqueId()).containsK
174182
getPluginInstance().getShopMemory().put(player.getUniqueId(), foundShopAtLocation.getShopId());
175183
}
176184
}
185+
}
186+
187+
188+
private float currentAngle = 0;
189+
190+
private void rotateDisplay(ItemDisplay display, Matrix4f mat,float scale,int duration){
191+
final float rotationIncrement = (float) Math.toRadians(2); // Rotate 2 degrees per tick
192+
193+
if (display == null || display.isDead() || !display.isValid()) { // display was removed from the world, abort task
194+
return;
195+
}
196+
197+
currentAngle += rotationIncrement; // Increment the angle
198+
if (currentAngle >= Math.toRadians(360)) {
199+
currentAngle -= (float) Math.toRadians(360); // Reset the angle if it completes a full rotation
200+
}
201+
202+
ItemStack itemStack = display.getItemStack();
203+
if (itemStack != null) {
204+
if (itemStack.getType().name().contains("SHIELD")) {
205+
return;
206+
}
207+
}
177208

178-
/* if (!isNew && getPluginInstance().getConfig().getBoolean("run-gc-immediately") && getGcCounter() >= 15) {
179-
setGcCounter(0);
180-
System.gc();
181-
}*/
209+
// Update the transformation matrix with the new rotation
210+
Bukkit.getScheduler().scheduleSyncDelayedTask(pluginInstance, ()->{
211+
display.setTransformationMatrix(mat.identity().scale(scale).rotateY(currentAngle));
212+
display.setInterpolationDelay(0); // no delay to the interpolation
213+
display.setInterpolationDuration(duration); // set the duration of the interpolated rotation
214+
});
182215
}
183216

184217
public void refreshShop(Shop shop) {
@@ -205,14 +238,6 @@ private void setPluginInstance(DisplayShops pluginInstance) {
205238
this.pluginInstance = pluginInstance;
206239
}
207240

208-
/*public int getViewDistance() {
209-
return viewDistance;
210-
}*/
211-
212-
/* private void setViewDistance(int viewDistance) {
213-
this.viewDistance = viewDistance;
214-
}*/
215-
216241
private boolean isAlwaysDisplay() {
217242
return alwaysDisplay;
218243
}
@@ -237,14 +262,6 @@ private void setPlayersToRefresh(LinkedList<UUID> playersToRefresh) {
237262
this.playersToRefresh = playersToRefresh;
238263
}
239264

240-
public int getGcCounter() {
241-
return gcCounter;
242-
}
243-
244-
public void setGcCounter(int gcCounter) {
245-
this.gcCounter = gcCounter;
246-
}
247-
248265
public boolean isPaused() {
249266
return pause;
250267
}

Core/src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ main: xzot1k.plugins.ds.DisplayShops
33
version: ${project.version} #-${core.build}
44
api-version: 1.13
55
author: XZot1K
6+
authors: [petulikan1]
67
description: Create immersive simplistic shops with animations, efficient transaction handling, and much more!
78
softdepend: [ Vault, NBTAPI, HeadDatabase, Prisma, PlaceholderAPI, PlotSquared, BentoBox, BSkyBlock, BentoBox-BSkyBlock, Oneblock, CMI, Essentials, MultiWorld, Multiverse-Core,
89
ASkyBlock, USkyBlock, FabledSkyBlock, PlayerWorldsPro, WorldGuard, WorldEdit, ItemsAdder, PlayerPoints, eco, EcoBits, Geyser-Spigot, DecentHolograms, EliteEnchantment ]

0 commit comments

Comments
 (0)