Skip to content

Commit

Permalink
add toggle for old item editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Jul 27, 2024
1 parent d81038d commit a11adef
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public static void givePrize(@NotNull final Player player, @Nullable Prize prize

prize = prize.hasPermission(player) ? prize.getAlternativePrize() : prize;

for (ItemStack item : prize.getEditorItems()) {
if (!MiscUtils.isInventoryFull(player)) {
player.getInventory().addItem(item);
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
}

if (!prize.getItemBuilders().isEmpty()) {
for (final ItemBuilder item : prize.getItemBuilders()) {
if (!MiscUtils.isInventoryFull(player)) {
Expand Down
60 changes: 50 additions & 10 deletions src/main/java/com/badbones69/crazycrates/api/objects/Crate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.badbones69.crazycrates.api.builders.types.CrateTierMenu;
import com.badbones69.crazycrates.api.crates.CrateHologram;
import com.badbones69.crazycrates.api.enums.PersistentKeys;
import com.badbones69.crazycrates.config.ConfigManager;
import com.badbones69.crazycrates.config.impl.ConfigKeys;
import com.badbones69.crazycrates.tasks.BukkitUserManager;
import com.badbones69.crazycrates.tasks.crates.CrateManager;
import com.badbones69.crazycrates.tasks.crates.effects.SoundEffect;
Expand All @@ -15,6 +17,7 @@
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
Expand Down Expand Up @@ -665,26 +668,63 @@ private void setItem(@Nullable final ItemStack itemStack, @NotNull final String

final String tiers = getPath(prizeName, "Tiers");

final boolean useOldEditor = ConfigManager.getConfig().getProperty(ConfigKeys.item_editor_toggle);

if (!section.contains(prizeName)) {
section.set(getPath(prizeName, "MaxRange"), 100);
}

String toBase64 = ItemUtil.toBase64(itemStack);
if (useOldEditor) {
final List<ItemStack> editorItems = new ArrayList<>();

if (section.contains(prizeName + ".Editor-Items")) {
section.getList(prizeName + ".Editor-Items").forEach(item -> editorItems.add((ItemStack) item));
}

editorItems.add(itemStack);

List<String> enchantments = new ArrayList<>();

section.set(getPath(prizeName, "DisplayData"), toBase64);
for (Enchantment enchantment : itemStack.getEnchantments().keySet()) {
enchantments.add(enchantment.getKey().getKey());
}

if (!enchantments.isEmpty()) section.set(getPath(prizeName, "DisplayEnchantments"), enchantments);

final String items = getPath(prizeName, "Items");
if (itemStack.hasItemMeta()) {
final ItemMeta itemMeta = itemStack.getItemMeta();

if (section.contains(items)) {
final List<String> list = section.getStringList(items);
if (itemMeta.hasDisplayName()) {
section.set(getPath(prizeName, "DisplayName"), itemMeta.getDisplayName());
}

list.add("Data:" + toBase64);
if (itemMeta.hasLore()) {
section.set(getPath(prizeName, "DisplayLore"), itemMeta.getLore());
}
}

section.set(items, list);
section.set(getPath(prizeName, "DisplayItem"), itemStack.getType().getKey().getKey());
section.set(getPath(prizeName, "DisplayAmount"), itemStack.getAmount());

section.set(getPath(prizeName, "Editor-Items"), editorItems);
} else {
section.set(items, new ArrayList<>() {{
add("Data:" + toBase64);
}});
String toBase64 = ItemUtil.toBase64(itemStack);

section.set(getPath(prizeName, "DisplayData"), toBase64);

final String items = getPath(prizeName, "Items");

if (section.contains(items)) {
final List<String> list = section.getStringList(items);

list.add("Data:" + toBase64);

section.set(items, list);
} else {
section.set(items, new ArrayList<>() {{
add("Data:" + toBase64);
}});
}
}

section.set(getPath(prizeName, "Chance"), chance);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/badbones69/crazycrates/api/objects/Prize.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class Prize {
private List<Tier> tiers = new ArrayList<>();
private Prize alternativePrize;

public Prize(@NotNull final ConfigurationSection section, @NotNull final List<Tier> tierPrizes, @NotNull final String crateName, @Nullable final Prize alternativePrize) {
private List<ItemStack> editorItems = new ArrayList<>();

public Prize(@NotNull final ConfigurationSection section, List<ItemStack> editorItems, @NotNull final List<Tier> tierPrizes, @NotNull final String crateName, @Nullable final Prize alternativePrize) {
this.section = section;

this.sectionName = section.getName();
Expand Down Expand Up @@ -73,6 +75,8 @@ public Prize(@NotNull final ConfigurationSection section, @NotNull final List<Ti

this.prizeItem = display();
this.displayItem = new ItemBuilder(this.prizeItem, true);

this.editorItems = editorItems;
}

/**
Expand Down Expand Up @@ -330,4 +334,8 @@ public final boolean hasPermission(@NotNull final Player player) {
}});
}
}

public final List<ItemStack> getEditorItems() {
return this.editorItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public void registerComments(CommentsConfiguration conf) {
})
public static final Property<Boolean> minimessage_toggle = newProperty("root.use-minimessage", true);

@Comment({
"This option defines what in-game editor format is used in CrazyCrates",
"",
"true -> uses the old one",
"false -> uses the new one"
})
public static final Property<Boolean> item_editor_toggle = newProperty("root.use-old-editor", false);

@Comment({
"This option will tell the plugin to send all messages as action bars or messages in chat.",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@
import java.util.TimerTask;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CrateManager {

Expand Down Expand Up @@ -152,6 +150,14 @@ public void reloadCrate(@Nullable final Crate crate) {
List<Tier> tierPrizes = new ArrayList<>();

if (prizeSection != null) {
final List<ItemStack> editorItems = new ArrayList<>();

if (prizeSection.contains("Editor-Items")) {
for (Object key : prizeSection.getList("Editor-Items")) {
editorItems.add((ItemStack) key);
}
}

for (String tier : prizeSection.getStringList("Tiers")) {
for (Tier key : crate.getTiers()) {
if (key.getName().equalsIgnoreCase(tier)) {
Expand All @@ -174,7 +180,7 @@ public void reloadCrate(@Nullable final Crate crate) {

prizes.add(new Prize(
prizeSection,
tierPrizes,
editorItems, tierPrizes,
crate.getName(),
alternativePrize
));
Expand Down Expand Up @@ -322,6 +328,14 @@ public void loadCrates() {
Prize alternativePrize = null;

if (prizeSection != null) {
final List<ItemStack> editorItems = new ArrayList<>();

if (prizeSection.contains("Editor-Items")) {
for (Object key : prizeSection.getList("Editor-Items")) {
editorItems.add((ItemStack) key);
}
}

for (String tier : prizeSection.getStringList("Tiers")) {
for (Tier key : tiers) {
if (key.getName().equalsIgnoreCase(tier)) {
Expand Down Expand Up @@ -360,7 +374,7 @@ public void loadCrates() {
}
}*/

prizes.add(new Prize(prizeSection, tierPrizes, crateName, alternativePrize));
prizes.add(new Prize(prizeSection, editorItems, tierPrizes, crateName, alternativePrize));
}
}
}
Expand Down

0 comments on commit a11adef

Please sign in to comment.