Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit

Permalink
Add glowing item support
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite committed Oct 28, 2021
1 parent 49f60b6 commit db04e02
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 24 deletions.
Binary file added pictures/banner features.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner previews.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner servers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner sponsors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner support.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner supported plugins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/banner template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions pictures/banner template.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 18 additions & 6 deletions src/main/java/dev/efnilite/witp/WITP.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.efnilite.witp.util.config.Configuration;
import dev.efnilite.witp.util.config.Option;
import dev.efnilite.witp.util.inventory.InventoryBuilder;
import dev.efnilite.witp.util.inventory.enchantment.GlowEnchant;
import dev.efnilite.witp.util.sql.Database;
import dev.efnilite.witp.util.sql.InvalidStatementException;
import dev.efnilite.witp.util.task.Tasks;
Expand Down Expand Up @@ -138,17 +139,21 @@ public void onEnable() {
return joins;
}));
long time = Tasks.end("load");

// ----- Enchantments -----
try {
new GlowEnchant();
} catch (IllegalArgumentException ex) {
Verbose.warn("Reload detected!");
Verbose.warn("Reloading this plugin might cause issues!");
return;
}

Verbose.info("Loaded WITP in " + time + "ms!");
}

@Override
public void onDisable() {
HandlerList.unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this);
if (database != null) {
database.close();
}

for (ParkourUser user : ParkourUser.getUsers()) {
try {
ParkourUser.unregister(user, true, false, false);
Expand All @@ -157,6 +162,13 @@ public void onDisable() {
Verbose.error("Error while unregistering");
}
}

HandlerList.unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this);
if (database != null) {
database.close();
}

if (divider != null) { // somehow this can be null despite it only ever being set to a new instance?
for (Player player : divider.getWorld().getPlayers()) {
player.kickPlayer("Server is restarting");
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/dev/efnilite/witp/generator/DefaultGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ public void generate() {
}

public void altMenu() {
// to be inherited
}

public boolean hasAltMenu() {
return false;
}

@Override
Expand Down Expand Up @@ -652,7 +656,7 @@ public void menu(String... optDisabled) {
});
i++;
}
styling.setItem(26, close, (t2, e2) -> menu());
styling.setItem(26, close, (t2, e2) -> menu(optDisabled));
styling.build();
});
}
Expand All @@ -668,7 +672,7 @@ public void menu(String... optDisabled) {
}
});
}
lead.setItem(26, close, (t2, e2) -> menu());
lead.setItem(26, close, (t2, e2) -> menu(optDisabled));
lead.build();
});
}
Expand All @@ -687,7 +691,7 @@ public void menu(String... optDisabled) {
});
i++;
}
timeofday.setItem(26, close, (t2, e2) -> menu());
timeofday.setItem(26, close, (t2, e2) -> menu(optDisabled));
timeofday.build();
});
}
Expand All @@ -704,7 +708,7 @@ public void menu(String... optDisabled) {
builder.setItem(dynamic.next(), item, (t2, e2) -> {
pp.useParticles = !pp.useParticles;
pp.sendTranslated("selected-particles", normalizeBoolean(Util.colorBoolean(Util.reverseBoolean(particlesString))));
menu();
menu(optDisabled);
});
}
if (checkOptions("scoreboard", "witp.scoreboard", disabled) && Option.SCOREBOARD) {
Expand All @@ -720,7 +724,7 @@ public void menu(String... optDisabled) {
pp.getBoard().delete();
}
pp.sendTranslated("selected-scoreboard", normalizeBoolean(Util.colorBoolean(Util.reverseBoolean(scoreboardString))));
menu();
menu(optDisabled);
});
}
if (checkOptions("death-msg", "witp.fall", disabled)) {
Expand All @@ -730,20 +734,20 @@ public void menu(String... optDisabled) {
builder.setItem(dynamic.next(), item, (t2, e2) -> {
pp.showDeathMsg = !pp.showDeathMsg;
pp.sendTranslated("selected-fall-message", normalizeBoolean(Util.colorBoolean(Util.reverseBoolean(deathString))));
menu();
menu(optDisabled);
});
}
if (checkOptions("special", "witp.special", disabled)) {
String specialString = Boolean.toString(pp.useSpecial);
item = config.getFromItemData(pp.locale, "options.special", normalizeBoolean(Util.colorBoolean(specialString)));
item.setType(pp.useSpecial ? Material.GREEN_WOOL : Material.RED_WOOL);
builder.setItem(dynamic.next(), item, (t2, e2) -> askReset("special"));
builder.setItem(dynamic.next(), item, (t2, e2) -> askReset("special", optDisabled));
}
if (checkOptions("structure", "witp.structures", disabled)) {
String structuresString = Boolean.toString(pp.useStructure);
item = config.getFromItemData(pp.locale, "options.structure", normalizeBoolean(Util.colorBoolean(structuresString)));
item.setType(pp.useStructure ? Material.GREEN_WOOL : Material.RED_WOOL);
builder.setItem(dynamic.next(), item, (t2, e2) -> askReset("structure"));
builder.setItem(dynamic.next(), item, (t2, e2) -> askReset("structure", optDisabled));
}

if (checkOptions("gamemode", "witp.gamemode", disabled)) {
Expand All @@ -770,7 +774,7 @@ public void menu(String... optDisabled) {
pp.sendTranslated("selected-language", langName);
});
}
language.setItem(26, close, (t3, e3) -> menu());
language.setItem(26, close, (t3, e3) -> menu(optDisabled));
language.build();
});
}
Expand Down Expand Up @@ -812,9 +816,9 @@ private void difficultyMenu() {
difficulty.build();
}

private void askReset(String item) {
private void askReset(String item, String... optDisabled) {
if (pp.getGenerator().score < 25) {
confirmReset(item);
confirmReset(item, optDisabled);
return;
}
pp.sendTranslated("confirm");
Expand All @@ -835,17 +839,17 @@ private boolean checkOptions(String option, @Nullable String perm, List<String>
}
}

public void confirmReset(String item) {
public void confirmReset(String item, String... optDisabled) {
switch (item) {
case "structure":
pp.useStructure = !pp.useStructure;
pp.sendTranslated("selected-structures", normalizeBoolean(Util.colorBoolean(Boolean.toString(pp.useStructure))));
pp.getGenerator().menu();
menu(optDisabled);
break;
case "special":
pp.useSpecial = !pp.useSpecial;
pp.sendTranslated("selected-special-blocks", normalizeBoolean(Util.colorBoolean(Boolean.toString(pp.useSpecial))));
pp.getGenerator().menu();
menu(optDisabled);
break;
case "e-difficulty":
pp.difficulty = 0.3;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/dev/efnilite/witp/util/inventory/ItemBuilder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.efnilite.witp.util.inventory;

import dev.efnilite.witp.util.Util;
import dev.efnilite.witp.util.inventory.enchantment.GlowEnchant;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -26,6 +28,7 @@ public class ItemBuilder {

private int amount;
private int durability;
private boolean glowing;
private boolean unbreakable;
private String name;
private final List<String> lore;
Expand Down Expand Up @@ -88,7 +91,12 @@ public ItemStack build() {
ItemMeta meta = Bukkit.getItemFactory().getItemMeta(item.getType());
assert meta != null;

meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
if (glowing) {
meta.addEnchant(GlowEnchant.getEnchantment(), 1, false);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}

meta.setDisplayName(Util.color(name));
meta.setLore(lore);
((Damageable) meta).setDamage(Math.abs(durability - type.getMaxDurability()));
meta.setUnbreakable(unbreakable);
Expand Down Expand Up @@ -149,6 +157,7 @@ public ItemBuilder unbreakable() {
*/
public ItemBuilder glowing(boolean predicate) {
if (predicate) {
this.glowing = true;
}
return this;
}
Expand All @@ -159,6 +168,7 @@ public ItemBuilder glowing(boolean predicate) {
* @return the instance
*/
public ItemBuilder glowing() {
this.glowing = true;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.efnilite.witp.util.inventory.enchantment;

import dev.efnilite.witp.util.Verbose;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;

/**
* Wrapper for enchantments
*/
public abstract class EnchantmentWrapper extends Enchantment {

protected String name;
protected static Enchantment enchantment;

public EnchantmentWrapper(String name, Plugin plugin) {
super(new NamespacedKey(plugin, name));
this.name = name;

enchantment = this;

register();
}

public void register() {
if (!Enchantment.isAcceptingRegistrations()) {
try {
Field field = Enchantment.class.getDeclaredField("acceptingNew");
field.setAccessible(true);
field.set(null, true);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
Verbose.error("Couldn't init Enchantment");
return;
}

}
Enchantment.registerEnchantment(this);
}

@Override
public @NotNull String getName() {
return name;
}

@Override
public boolean isTreasure() {
return false;
}

@Override
public boolean isCursed() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.efnilite.witp.util.inventory.enchantment;

import dev.efnilite.witp.WITP;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public class GlowEnchant extends EnchantmentWrapper {

public GlowEnchant() {
super("glow", WITP.getInstance());
}

@Override
public int getMaxLevel() {
return 1;
}

@Override
public int getStartLevel() {
return 1;
}

@Override
public @NotNull EnchantmentTarget getItemTarget() {
return EnchantmentTarget.ALL;
}

@Override
public boolean conflictsWith(@NotNull Enchantment enchantment) {
return false;
}

@Override
public boolean canEnchantItem(@NotNull ItemStack itemStack) {
return true;
}

public static Enchantment getEnchantment() {
return enchantment;
}
}
6 changes: 3 additions & 3 deletions src/main/resources/items.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Options to change all the items
# In lores, || is used to create a new line
# Colour codes can be created using '&'
# Items need be from https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html
# Colour codes can be created using '&' or hex '&#abcdef'
# Item materials need be from https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html

items:
general:
Expand Down Expand Up @@ -68,7 +68,7 @@ locale:
name: '&c&lClose'
lore: ''
menu:
name: '&c&lOptions'
name: '&c&l-= Options =-'
lore: ''

gamemodes:
Expand Down

0 comments on commit db04e02

Please sign in to comment.