Skip to content

Commit

Permalink
Remove nessesery class and add commands into items
Browse files Browse the repository at this point in the history
  • Loading branch information
eripe14 committed Oct 21, 2023
1 parent aa2374e commit 5b78896
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package com.eternalcode.core.feature.warp.config;
package com.eternalcode.core.configuration.contextual;

import net.dzikoysk.cdn.entity.Contextual;
import net.dzikoysk.cdn.entity.Description;
import org.bukkit.Material;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Contextual
public class WarpConfigItem {

public String name = "&6Warp";
public List<String> lore = Collections.singletonList("&7Click to teleport to warp");
public class ConfigItem {

public String name = "&6Item";
public List<String> lore = Collections.singletonList("&7Default lore");
public Material material = Material.PLAYER_HEAD;
public String texture = "none";
public boolean glow = true;

public int slot = 10;
public boolean glow = false;
public int slot = 0;
@Description("What command should be executed when the item is clicked? If it is not to be performed, leave it blank")
public List<String> commands = new ArrayList<>();

public WarpConfigItem(String name, List<String> lore, Material material, String texture, boolean glow, int slot) {
public ConfigItem(String name, List<String> lore, Material material, String texture, boolean glow, int slot, List<String> commands) {
this.name = name;
this.lore = lore;
this.material = material;
this.texture = texture;
this.glow = glow;
this.slot = slot;
this.commands = commands;
}

public WarpConfigItem() {
public ConfigItem() {

}

Expand Down Expand Up @@ -55,52 +58,61 @@ public int slot() {
return this.slot;
}

public List<String> commands() {
return this.commands;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {

private final WarpConfigItem warpConfigItem = new WarpConfigItem();
private final ConfigItem configItem = new ConfigItem();

public Builder withName(String name) {
this.warpConfigItem.name = name;
this.configItem.name = name;

return this;
}

public Builder withLore(List<String> lore) {
this.warpConfigItem.lore = lore;
this.configItem.lore = lore;

return this;
}

public Builder withMaterial(Material material) {
this.warpConfigItem.material = material;
this.configItem.material = material;

return this;
}

public Builder withTexture(String texture) {
this.warpConfigItem.texture = texture;
this.configItem.texture = texture;

return this;
}

public Builder withGlow(boolean glow) {
this.warpConfigItem.glow = glow;
this.configItem.glow = glow;

return this;
}

public Builder withSlot(int slot) {
this.warpConfigItem.slot = slot;
this.configItem.slot = slot;

return this;
}

public Builder withCommands(List<String> commands) {
this.configItem.commands = commands;

return this;
}

public WarpConfigItem build() {
return this.warpConfigItem;
public ConfigItem build() {
return this.configItem;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.core.configuration.implementation;

import com.eternalcode.core.adventure.legacy.LegacyColorProcessor;
import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.database.DatabaseType;
import com.eternalcode.core.feature.afk.AfkSettings;
Expand All @@ -15,6 +16,7 @@
import net.dzikoysk.cdn.entity.Exclude;
import net.dzikoysk.cdn.source.Resource;
import net.dzikoysk.cdn.source.Source;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Sound;

import java.io.File;
Expand Down Expand Up @@ -311,7 +313,6 @@ public static class Butcher {

@Contextual
public static class AutoMessage implements AutoMessageSettings {

@Description("# AutoMessage should be enabled?")
public boolean enabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.user.User;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.permission.Permission;
import dev.rollczi.litecommands.command.route.Route;
Expand All @@ -20,8 +21,8 @@ class LanguageCommand {

@Execute
@DescriptionDocs(description = "Open language inventory")
void execute(Player player) {
this.languageInventory.open(player);
void execute(Player player, User user) {
this.languageInventory.open(player, user.getLanguage());
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package com.eternalcode.core.feature.language;

import com.eternalcode.annotations.scan.feature.FeatureDocs;
import com.eternalcode.core.configuration.contextual.ConfigItem;
import com.eternalcode.core.feature.language.config.LanguageConfiguration;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.feature.language.config.LanguageConfigItem;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.user.User;
import com.eternalcode.core.user.UserManager;
import com.eternalcode.core.util.AdventureUtil;
import dev.triumphteam.gui.builder.item.BaseItemBuilder;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Optional;
Expand All @@ -30,20 +34,26 @@
class LanguageInventory {

private final LanguageConfiguration languageConfiguration;
private final TranslationManager translationManager;
private final NoticeService noticeService;
private final UserManager userManager;
private final Server server;
private final MiniMessage miniMessage;

@Inject
LanguageInventory(LanguageConfiguration languageConfiguration, NoticeService noticeService, UserManager userManager, MiniMessage miniMessage) {
LanguageInventory(LanguageConfiguration languageConfiguration, TranslationManager translationManager, NoticeService noticeService, UserManager userManager, Server server, MiniMessage miniMessage) {
this.languageConfiguration = languageConfiguration;
this.translationManager = translationManager;
this.noticeService = noticeService;
this.userManager = userManager;
this.server = server;
this.miniMessage = miniMessage;
}

void open(Player player) {
void open(Player player, Language language) {
LanguageConfiguration.LanguageSelector languageSelector = this.languageConfiguration.languageSelector;
Translation translation = this.translationManager.getMessages(language);
Translation.LanguageSection languageSection = translation.language();

Gui gui = Gui.gui()
.title(this.miniMessage.deserialize(languageSelector.title))
Expand Down Expand Up @@ -84,30 +94,8 @@ void open(Player player) {
}

for (LanguageConfigItem languageConfigItem : languageSelector.languageConfigItemMap) {
Component name = AdventureUtil.RESET_ITEM.append(this.miniMessage.deserialize(languageConfigItem.name));

List<Component> lore = languageConfigItem.lore
.stream()
.map(entry -> AdventureUtil.RESET_ITEM.append(this.miniMessage.deserialize(entry)))
.collect(Collectors.toList());

ItemStack item;

if (languageConfigItem.material == Material.PLAYER_HEAD) {
item = ItemBuilder.skull()
.name(name)
.lore(lore)
.texture(languageConfigItem.texture)
.build();
}
else {
item = ItemBuilder.from(languageConfigItem.material)
.name(name)
.lore(lore)
.build();
}

GuiItem guiItem = new GuiItem(item);
BaseItemBuilder baseItemBuilder = this.createItem(languageConfigItem);
GuiItem guiItem = baseItemBuilder.asGuiItem();

guiItem.setAction(event -> {
user.getSettings().setLanguage(languageConfigItem.language);
Expand All @@ -116,13 +104,53 @@ void open(Player player) {

this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.language().languageChanged())
.notice(translations -> translations.language().languageChanged())
.send();
});

gui.setItem(languageConfigItem.slot, guiItem);
}

for (ConfigItem item : languageSection.decorationItems()) {
BaseItemBuilder baseItemBuilder = this.createItem(item);
GuiItem guiItem = baseItemBuilder.asGuiItem();

guiItem.setAction(event -> {
if (item.commands.isEmpty()) {
return;
}

for (String command : item.commands) {
this.server.dispatchCommand(player, command);
}

player.closeInventory();
});

gui.setItem(item.slot(), guiItem);
}

gui.open(player);
}

private BaseItemBuilder createItem(ConfigItem item) {
Component name = AdventureUtil.RESET_ITEM.append(this.miniMessage.deserialize(item.name()));
List<Component> lore = item.lore()
.stream()
.map(entry -> AdventureUtil.RESET_ITEM.append(this.miniMessage.deserialize(entry)))
.toList();

if (item.material() == Material.PLAYER_HEAD && !item.texture().isEmpty()) {
return ItemBuilder.skull()
.name(name)
.lore(lore)
.texture(item.texture())
.glow(item.glow());
}

return ItemBuilder.from(item.material())
.name(name)
.lore(lore)
.glow(item.glow());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.core.feature.language.config;

import com.eternalcode.core.configuration.contextual.ConfigItem;
import com.eternalcode.core.feature.language.Language;
import net.dzikoysk.cdn.entity.Contextual;
import org.bukkit.Material;
Expand All @@ -8,38 +9,15 @@
import java.util.List;

@Contextual
public class LanguageConfigItem {

public Material material = Material.DIRT;
public class LanguageConfigItem extends ConfigItem {

public Language language = Language.EN;

public int slot = 1;

public String name = "none";

public List<String> lore = Collections.singletonList("none");

public String texture = "none";

public LanguageConfigItem(Material material, Language language, int slot, String name, List<String> lore, String texture) {
this.material = material;
public LanguageConfigItem(String name, List<String> lore, Material material, String texture, boolean glow, int slot, List<String> commands, Language language) {
super(name, lore, material, texture, glow, slot, commands);
this.language = language;
this.slot = slot;
this.name = name;
this.lore = lore;
this.texture = texture;
}

public LanguageConfigItem(Material material, Language language, int slot, String name, List<String> lore) {
this.material = material;
this.language = language;
this.slot = slot;
this.name = name;
this.lore = lore;
}

public LanguageConfigItem() {

}
}
Loading

0 comments on commit 5b78896

Please sign in to comment.