mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add select enchant setting gui & fix a typo.
This commit is contained in:
parent
e99fd4d640
commit
7129aed585
4 changed files with 168 additions and 8 deletions
|
|
@ -26,6 +26,12 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends ValueUpdatableGui {
|
public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends ValueUpdatableGui {
|
||||||
|
|
||||||
|
public static final List<Enchantment> SORTED_ENCHANTMENT_LIST;
|
||||||
|
static {
|
||||||
|
SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values());
|
||||||
|
SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
|
||||||
|
}
|
||||||
|
|
||||||
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
|
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
|
||||||
|
|
||||||
private final Gui backGui;
|
private final Gui backGui;
|
||||||
|
|
@ -90,10 +96,7 @@ public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.Sett
|
||||||
protected void prepareValues(){
|
protected void prepareValues(){
|
||||||
bookItemFactoryList = new ArrayList<>();
|
bookItemFactoryList = new ArrayList<>();
|
||||||
|
|
||||||
List<Enchantment> enchantments = Arrays.asList(Enchantment.values());
|
for (Enchantment enchant : SORTED_ENCHANTMENT_LIST) {
|
||||||
enchantments.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
|
|
||||||
|
|
||||||
for (Enchantment enchant : enchantments) {
|
|
||||||
T factory = getFactoryFromEnchant(enchant);
|
T factory = getFactoryFromEnchant(enchant);
|
||||||
|
|
||||||
bookItemFactoryList.add(factory);
|
bookItemFactoryList.add(factory);
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ import java.util.Set;
|
||||||
|
|
||||||
public interface SelectEnchantmentContainer {
|
public interface SelectEnchantmentContainer {
|
||||||
|
|
||||||
List<Enchantment> getSelectedEnchantments();
|
Set<Enchantment> getSelectedEnchantments();
|
||||||
|
|
||||||
boolean setSelectedEnchantments(List<Enchantment> enchantments);
|
boolean setSelectedEnchantments(Set<Enchantment> enchantments);
|
||||||
|
|
||||||
Set<Enchantment> illegalEnchantments();
|
Set<Enchantment> illegalEnchantments();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,157 @@
|
||||||
|
package xyz.alexcrea.cuanvil.gui.config.settings;
|
||||||
|
|
||||||
|
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||||
|
import com.github.stefvanschie.inventoryframework.pane.Orientable;
|
||||||
|
import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
|
||||||
|
import com.github.stefvanschie.inventoryframework.pane.Pane;
|
||||||
|
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||||
|
import io.delilaheve.CustomAnvil;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.inventory.ItemFlag;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
|
||||||
|
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||||
|
import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui;
|
||||||
|
import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer;
|
||||||
|
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class EnchantSelectSettingGui extends AbstractSettingGui{
|
||||||
|
|
||||||
|
SelectEnchantmentContainer enchantContainer;
|
||||||
|
int page;
|
||||||
|
|
||||||
|
Set<Enchantment> selectedEnchant;
|
||||||
|
|
||||||
|
public EnchantSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectEnchantmentContainer enchantContainer, int page) {
|
||||||
|
super(6, title, parent);
|
||||||
|
this.enchantContainer = enchantContainer;
|
||||||
|
// Not used and not planned rn
|
||||||
|
this.page = page;
|
||||||
|
|
||||||
|
this.selectedEnchant = new HashSet<>(enchantContainer.getSelectedEnchantments());
|
||||||
|
|
||||||
|
initGroups();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Pattern getGuiPattern() {
|
||||||
|
return new Pattern(
|
||||||
|
"000000000",
|
||||||
|
"000000000",
|
||||||
|
"000000000",
|
||||||
|
"000000000",
|
||||||
|
"000000000",
|
||||||
|
"B1111111S"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initGroups(){
|
||||||
|
// Add enchantment gui item
|
||||||
|
OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5);
|
||||||
|
filledEnchant.setPriority(Pane.Priority.HIGH);
|
||||||
|
filledEnchant.align(OutlinePane.Alignment.BEGIN);
|
||||||
|
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
|
||||||
|
|
||||||
|
Set<Enchantment> illegalEnchant = this.enchantContainer.illegalEnchantments();
|
||||||
|
for (Enchantment enchant : AbstractEnchantConfigGui.SORTED_ENCHANTMENT_LIST) {
|
||||||
|
if(illegalEnchant.contains(enchant)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
filledEnchant.addItem(getGuiItemFromEnchant(enchant));
|
||||||
|
}
|
||||||
|
|
||||||
|
addPane(filledEnchant);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private GuiItem getGuiItemFromEnchant(Enchantment enchantment){
|
||||||
|
boolean isIn = this.selectedEnchant.contains(enchantment);
|
||||||
|
|
||||||
|
Material usedMaterial;
|
||||||
|
if(isIn){
|
||||||
|
usedMaterial = Material.ENCHANTED_BOOK;
|
||||||
|
}else{
|
||||||
|
usedMaterial = Material.BOOK;
|
||||||
|
}
|
||||||
|
ItemStack item = new ItemStack(usedMaterial);
|
||||||
|
|
||||||
|
setEnchantItemMeta(item, enchantment.getKey().getKey(), isIn);
|
||||||
|
|
||||||
|
GuiItem guiItem = new GuiItem(item, CustomAnvil.instance);
|
||||||
|
guiItem.setAction(getEnchantItemConsumer(enchantment, guiItem));
|
||||||
|
return guiItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final List<String> TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected");
|
||||||
|
private static final List<String> FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected");
|
||||||
|
|
||||||
|
public void setEnchantItemMeta(ItemStack item, String name, boolean isIn){
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
|
||||||
|
if(meta == null){
|
||||||
|
CustomAnvil.instance.getLogger().warning("Could not create item for enchantment: "+name+":\n" +
|
||||||
|
"Item do not gave item meta: "+item+". Using placeholder instead");
|
||||||
|
item.setType(Material.PAPER);
|
||||||
|
meta = item.getItemMeta();
|
||||||
|
assert meta != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name));
|
||||||
|
if(isIn){
|
||||||
|
meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true);
|
||||||
|
meta.setLore(TRUE_LORE);
|
||||||
|
}else{
|
||||||
|
meta.removeEnchant(Enchantment.DAMAGE_UNDEAD);
|
||||||
|
meta.setLore(FALSE_LORE);
|
||||||
|
}
|
||||||
|
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS);
|
||||||
|
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Consumer<InventoryClickEvent> getEnchantItemConsumer(Enchantment enchant, GuiItem guiItem){
|
||||||
|
return event -> {
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
ItemStack item = guiItem.getItem();
|
||||||
|
|
||||||
|
boolean isIn = this.selectedEnchant.contains(enchant);
|
||||||
|
if(isIn){
|
||||||
|
this.selectedEnchant.remove(enchant);
|
||||||
|
item.setType(Material.BOOK);
|
||||||
|
}else{
|
||||||
|
this.selectedEnchant.add(enchant);
|
||||||
|
item.setType(Material.ENCHANTED_BOOK);
|
||||||
|
}
|
||||||
|
|
||||||
|
setEnchantItemMeta(item, enchant.getKey().getKey(), !isIn);
|
||||||
|
guiItem.setItem(item);// Just in case
|
||||||
|
|
||||||
|
update();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onSave() {
|
||||||
|
return this.enchantContainer.setSelectedEnchantments(this.selectedEnchant);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hadChange() {
|
||||||
|
Set<Enchantment> baseGroup = this.enchantContainer.getSelectedEnchantments();
|
||||||
|
return baseGroup.size() != this.selectedEnchant.size() ||
|
||||||
|
!baseGroup.containsAll(this.selectedEnchant);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -80,7 +80,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
|
||||||
setGroupItemMeta(item, group.getName(), isIn);
|
setGroupItemMeta(item, group.getName(), isIn);
|
||||||
|
|
||||||
GuiItem guiItem = new GuiItem(item, CustomAnvil.instance);
|
GuiItem guiItem = new GuiItem(item, CustomAnvil.instance);
|
||||||
guiItem.setAction(getGrpupItemConsumer(group, guiItem));
|
guiItem.setAction(getGroupItemConsumer(group, guiItem));
|
||||||
return guiItem;
|
return guiItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,7 +111,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
|
||||||
item.setItemMeta(meta);
|
item.setItemMeta(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Consumer<InventoryClickEvent> getGrpupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){
|
private Consumer<InventoryClickEvent> getGroupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){
|
||||||
return event -> {
|
return event -> {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue