Skip to content

Commit

Permalink
Simplify configs
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Sep 15, 2024
1 parent 79ec4e2 commit 1d1774f
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ public LovelyDropCommand(PluginConfig config, MessageFacade messageFacade) {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (!sender.hasPermission("lovelydrop.reload")) {
this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getNoPermissions());
this.messageFacade.sendMessageAsync(sender, this.config.messages.noPermissions);
return true;
}

if (args.length == 0) {
this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getNotEnoughArguments());
this.messageFacade.sendMessageAsync(sender, this.config.messages.notEnoughArguments);
return true;
}

if (!args[0].equalsIgnoreCase("reload")) {
this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getNotValidArgument());
this.messageFacade.sendMessageAsync(sender, this.config.messages.notValidArgument);
return true;
}

this.config.load();

this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getPluginReloaded());
this.messageFacade.sendMessageAsync(sender, this.config.messages.pluginReloaded);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ public void start() {
}

SpigotAdapter spigotAdapter = this.prepareSpigotAdapter();
this.logger.info("Using server engine adapter for version v" + spigotAdapter.getVersion());

MessageFacade messageFacade = new MessageFacade(this);

Storage storage;
try {
storage = new StorageFactory(config.getStorage(), this.getDataFolder()).createStorage();
storage = new StorageFactory(config.storage, this.getDataFolder()).createStorage();
} catch (StorageException exception) {
this.logger.error("Could not create the storage.", exception);
this.shutdown();
Expand All @@ -106,7 +107,7 @@ public void start() {
UserSettingTask userSettingTask = new UserSettingTask(userSettingFacade);

BukkitScheduler scheduler = this.getServer().getScheduler();
this.userSettingBukkitTask = scheduler.runTaskTimerAsynchronously(this, userSettingTask, 20L, config.getUserSettingsSaveInterval() * 20L);
this.userSettingBukkitTask = scheduler.runTaskTimerAsynchronously(this, userSettingTask, 20L, config.userSettingsSaveInterval * 20L);

this.registerListeners(config, spigotAdapter, messageFacade, userSettingFacade);

Expand All @@ -131,7 +132,7 @@ public void registerListeners(PluginConfig config, SpigotAdapter spigotAdapter,
PluginManager pluginManager = this.getServer().getPluginManager();

Map<String, Boolean> defaultDropsToInventory = new HashMap<>();
config.getDrops().keySet().forEach(key -> defaultDropsToInventory.put(key, true));
config.drops.keySet().forEach(key -> defaultDropsToInventory.put(key, true));

this.userSettingListener = new UserSettingListener(this, userSettingFacade, defaultDropsToInventory);
pluginManager.registerEvents(this.userSettingListener, this);
Expand Down Expand Up @@ -163,7 +164,6 @@ public SpigotAdapter prepareSpigotAdapter() {
try {
namespaced = Class.forName("org.bukkit.NamespacedKey");
} catch (ClassNotFoundException exception) {
this.logger.info("Class 'NamespacedKey' not found on 1.12.2+. Using 1.8 spigot adapter.");
return new V1_8SpigotAdapter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public class PluginConfig extends OkaeriConfig {

@Comment("How often should all users settings be saved to the database (in seconds)?")
@Comment("By default it is 180 seconds (3 minutes).")
private long userSettingsSaveInterval = 180L;
public long userSettingsSaveInterval = 180L;

@Comment("")
@Comment("Whole configuration for the drop menu.")
private MenuConfig menu = new MenuConfig();
public MenuConfig menu = new MenuConfig();

@Comment("")
@Comment("All drops that will be available on the server.")
private Map<String, DropConfig> drops = new HashMap<String, DropConfig>() {{
public Map<String, DropConfig> drops = new HashMap<String, DropConfig>() {{
this.put(
"diamondSword",
new DropConfig(
Expand Down Expand Up @@ -195,51 +195,11 @@ public class PluginConfig extends OkaeriConfig {
}};

@Comment("")
private StorageConfig storage = new StorageConfig();
public StorageConfig storage = new StorageConfig();

@Comment("")
@Comment("All messages that can be sent by plugin to players.")
@Comment("If you want to disable specific messages just set them to '' (empty).")
private MessageConfig messages = new MessageConfig();

public long getUserSettingsSaveInterval() {
return this.userSettingsSaveInterval;
}

public void setUserSettingsSaveInterval(long userSettingsSaveInterval) {
this.userSettingsSaveInterval = userSettingsSaveInterval;
}

public MenuConfig getMenu() {
return this.menu;
}

public void setMenu(MenuConfig menu) {
this.menu = menu;
}

public Map<String, DropConfig> getDrops() {
return this.drops;
}

public void setDrops(Map<String, DropConfig> drops) {
this.drops = drops;
}

public StorageConfig getStorage() {
return this.storage;
}

public void setStorage(StorageConfig storage) {
this.storage = storage;
}

public MessageConfig getMessages() {
return this.messages;
}

public void setMessages(MessageConfig messages) {
this.messages = messages;
}
public MessageConfig messages = new MessageConfig();

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public DropCommand(PluginConfig config, MessageFacade messageFacade, MenuFacade
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
// Check if sender is console.
if (!(sender instanceof Player)) {
this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getExecutedAsConsole());
this.messageFacade.sendMessageAsync(sender, this.config.messages.executedAsConsole);
return true;
}

Player player = (Player) sender;

if (!player.hasPermission("lovelydrop.menu.open")) {
this.messageFacade.sendMessageAsync(sender, this.config.getMessages().getNoPermissions());
this.messageFacade.sendMessageAsync(sender, this.config.messages.noPermissions);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class DropConfig extends OkaeriConfig {
public static class FortuneConfig extends OkaeriConfig {

@Comment("Percentage chance of dropping the item.")
private FloatFormat chance;
public FloatFormat chance;

@Comment("")
@Comment("Range of the amount of items that can be dropped.")
private IntRange amount;
public IntRange amount;

@Comment("")
@Comment("Amount of experience given to the player when they have a successful drop.")
private int experience;
public int experience;

public FortuneConfig(float chance, IntRange amount, int experience) {
this(new FloatFormat(chance), amount, experience);
Expand All @@ -36,30 +36,6 @@ public FortuneConfig(FloatFormat chance, IntRange amount, int experience) {
this.experience = experience;
}

public FloatFormat getChance() {
return this.chance;
}

public void setChance(FloatFormat chance) {
this.chance = chance;
}

public IntRange getAmount() {
return this.amount;
}

public void setAmount(IntRange amount) {
this.amount = amount;
}

public int getExperience() {
return this.experience;
}

public void setExperience(int experience) {
this.experience = experience;
}

}

@Comment("Item representation of the drop.")
Expand All @@ -71,26 +47,26 @@ public void setExperience(int experience) {
@Comment("For example:")
@Comment("1.8 - damage_all | 1.8 - durability | 1.8 - loot_bonus_mobs")
@Comment("1.18 - sharpness | 1.18 - unbreaking | 1.18 - loot")
private ComplexItemStack item;
public ComplexItemStack item;

@Comment("")
@Comment("Item representation of the block from which 'item' should be dropped.")
@Comment("If you want to use legacy materials, set a durability to some number, e.g. material: stone with durability: 5 for andesite.")
@Comment("Check https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html for list of materials.")
private ItemStack source;
public ItemStack source;

@Comment("")
@Comment("Height range within which the item can be dropped.")
private IntRange height;
public IntRange height;

@Comment("")
@Comment("All properties will be applied to the drop, depending on the current pickaxe fortune level.")
@Comment("It must contain at least one section.")
private Map<Integer, FortuneConfig> fortune;
public Map<Integer, FortuneConfig> fortune;

@Comment("")
@Comment("List of biomes in which the drop will not be dropped.")
private Set<Biome> disabledBioms;
public Set<Biome> disabledBioms;

public DropConfig(ComplexItemStack item, ItemStack source, IntRange height, Map<Integer, FortuneConfig> fortune) {
this(item, source, height, fortune, new HashSet<>());
Expand All @@ -104,44 +80,4 @@ public DropConfig(ComplexItemStack item, ItemStack source, IntRange height, Map<
this.disabledBioms = disabledBioms;
}

public ComplexItemStack getItem() {
return this.item;
}

public void setItem(ComplexItemStack item) {
this.item = item;
}

public ItemStack getSource() {
return this.source.clone();
}

public void setSource(ItemStack source) {
this.source = source;
}

public IntRange getHeight() {
return this.height;
}

public void setHeight(IntRange height) {
this.height = height;
}

public Map<Integer, FortuneConfig> getFortune() {
return this.fortune;
}

public void setFortune(Map<Integer, FortuneConfig> fortune) {
this.fortune = fortune;
}

public Set<Biome> getDisabledBioms() {
return this.disabledBioms;
}

public void setDisabledBioms(Set<Biome> disabledBioms) {
this.disabledBioms = disabledBioms;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public void onSourceBreak(BlockBreakEvent event) {
Optional<UserSetting> userSettingMaybe = this.userSettingFacade.findUserSettingByPlayerIdFromCache(player.getUniqueId());
if (!userSettingMaybe.isPresent()) {
this.logger.error("User settings not found for {}.", player.getName());
this.messageFacade.sendMessageAsync(player, this.config.getMessages().getNeedToJoinAgain());
this.messageFacade.sendMessageAsync(player, this.config.messages.needToJoinAgain);
return;
}

UserSetting userSetting = userSettingMaybe.get();

// Get all drops from the source.
Set<Entry<String, DropConfig>> drops = this.config.getDrops().entrySet().stream()
Set<Entry<String, DropConfig>> drops = this.config.drops.entrySet().stream()
.filter(drop -> !userSetting.hasDisabledDrop(drop.getKey()))
.filter(drop -> !drop.getValue().getDisabledBioms().contains(block.getBiome()))
.filter(drop -> drop.getValue().getSource().getType() == block.getType())
.filter(drop -> drop.getValue().getSource().getDurability() == block.getData())
.filter(drop -> !drop.getValue().disabledBioms.contains(block.getBiome()))
.filter(drop -> drop.getValue().source.getType() == block.getType())
.filter(drop -> drop.getValue().source.getDurability() == block.getData())
.collect(Collectors.toSet());
if (drops.isEmpty()) {
return;
Expand All @@ -87,36 +87,36 @@ public void onSourceBreak(BlockBreakEvent event) {
drops.forEach(keyAndDrop -> {
DropConfig drop = keyAndDrop.getValue();

IntRange heightRange = drop.getHeight();
IntRange heightRange = drop.height;
if (blockHeight < heightRange.getMin() || blockHeight > heightRange.getMax()) {
return;
}

FortuneConfig fortune = drop.getFortune().getOrDefault(fortuneLevel, drop.getFortune().get(0));
FortuneConfig fortune = drop.fortune.getOrDefault(fortuneLevel, drop.fortune.get(0));
if (fortune == null) {
return;
}

if (fortune.getChance().getValue() <= Math.random() * 100.0F) {
if (fortune.chance.getValue() <= Math.random() * 100.0F) {
return;
}

ItemStack dropItem = drop.getItem().getItemStack();
ItemStack dropItem = drop.item.getItemStack();

// If pickaxe has silk touch and drop is cobblestone, change drop to stone.
if (dropItem.getType() == Material.COBBLESTONE && pickaxe.containsEnchantment(Enchantment.SILK_TOUCH)) {
dropItem.setType(Material.STONE);
}

// Give experience to player.
player.giveExp(fortune.getExperience());
player.giveExp(fortune.experience);

// Randomize amount from range and set it for item.
int amount = fortune.getAmount().getRandom();
int amount = fortune.amount.getRandom();
dropItem.setAmount(amount);

String[] placeholders = { "{DROP}", keyAndDrop.getKey(), "{AMOUNT}", String.valueOf(amount) };
this.messageFacade.sendMessage(player, this.config.getMessages().getDropSuccessful(), placeholders);
this.messageFacade.sendMessage(player, this.config.messages.dropSuccessful, placeholders);

World world = player.getWorld();

Expand Down
Loading

0 comments on commit 1d1774f

Please sign in to comment.