9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-29 11:59:21 +00:00

Added cool down of setting items

This commit is contained in:
HeroBrineGoat
2022-01-23 16:37:47 -05:00
parent eec23ab54b
commit aed81b4a13
4 changed files with 50 additions and 9 deletions

View File

@@ -33,6 +33,10 @@ public class CosmeticGui {
protected final Map<Integer, GuiItem> guiItemMap;
protected Gui gui;
private long lastClicked;
private static final float COOL_DOWN = 0.5f;
public CosmeticGui(
final HMCCosmetics plugin,
final String title,
@@ -72,6 +76,10 @@ public class CosmeticGui {
final InventoryClickEvent event,
final GuiAction<InventoryClickEvent> actionIfSet) {
final long current = System.currentTimeMillis();
if ((current - this.lastClicked) / 1000. < COOL_DOWN) return;
this.lastClicked = current;
if (!(human instanceof final Player player)) {
return;
}
@@ -129,10 +137,6 @@ public class CosmeticGui {
if (itemStack == null) return null;
guiItem.setItemStack(
ItemBuilder.from(itemStack.clone()).papiPlaceholders(player).build()
);
if (guiItem instanceof final ArmorItem armorItem) {
final String permission = armorItem.getPermission() == null ? "" : armorItem.getPermission();
@@ -159,6 +163,10 @@ public class CosmeticGui {
);
}
guiItem.setItemStack(
ItemBuilder.from(itemStack.clone()).papiPlaceholders(player).build()
);
return guiItem;
}
@@ -196,4 +204,13 @@ public class CosmeticGui {
papiPlaceholders(player).
build();
}
public CosmeticGui copy() {
return new CosmeticGui(
this.plugin,
this.title,
this.rows,
new HashMap<>(this.guiItemMap)
);
}
}

View File

@@ -9,6 +9,7 @@ import io.github.fisher2911.hmccosmetics.cosmetic.CosmeticManager;
import io.github.fisher2911.hmccosmetics.user.User;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
@@ -17,6 +18,7 @@ import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class CosmeticsMenu {
@@ -34,7 +36,7 @@ public class CosmeticsMenu {
}
public void openMenu(final String id, final HumanEntity humanEntity) {
final CosmeticGui cosmeticGui = this.guiMap.get(id);
final CosmeticGui cosmeticGui = this.getGui(id);
if (cosmeticGui != null) {
cosmeticGui.open(humanEntity);
@@ -59,13 +61,20 @@ public class CosmeticsMenu {
return;
}
final CosmeticGui gui = this.guiMap.get(DYE_MENU);
final CosmeticGui gui = this.getGui(DYE_MENU);
if (gui instanceof final DyeSelectorGui dyeSelectorGui) {
dyeSelectorGui.getGui(user, type).open(player);
}
}
@Nullable
private CosmeticGui getGui(final String id) {
final CosmeticGui gui = this.guiMap.get(id);
if (gui == null) return null;
return gui.copy();
}
public void load() {
this.guiMap.clear();
final File file = Path.of(this.plugin.getDataFolder().getPath(),

View File

@@ -1,6 +1,7 @@
package io.github.fisher2911.hmccosmetics.gui;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
@@ -16,6 +17,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -180,4 +182,15 @@ public class DyeSelectorGui extends CosmeticGui {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public DyeSelectorGui copy() {
return new DyeSelectorGui(
this.plugin,
super.title,
super.rows,
new HashMap<>(super.guiItemMap),
HashBiMap.create(this.cosmeticsSlots),
this.selectedCosmetic
);
}
}

View File

@@ -226,9 +226,11 @@ public class UserManager {
public void setItem(final User user, final ArmorItem armorItem) {
user.setItem(armorItem);
switch (armorItem.getType()) {
case HAT, OFF_HAND -> this.updateCosmetics(user);
}
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
switch (armorItem.getType()) {
case HAT, OFF_HAND -> this.updateCosmetics(user);
}
});
}
public void removeItem(final User user, final ArmorItem.Type type) {