Skip to content

Commit

Permalink
Merge pull request #24 from Florke64/refactoring
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
Florke64 authored Nov 20, 2024
2 parents 5b92679 + 86ed2b2 commit 9004d93
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 56 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ Paper Plugin adding stone generator machines. With custom drops and cool feature

Custom config, language file, two database connectors, admin "event" command is also there.

![[Drop Filter]](image/drop_filter.png)
`drop_filter.png`: drop filter

### Latest Release

> **Note:** This plugin is still in its development stage!
The latest release details can be seen [here](https://github.com/FlrQue/StoneAge/releases/latest)
(what's new and *.jar downloads).

API and Configuration is still subject to change.
![[Machine]](image/machine_upfacing.png)
`machine_upfacing.png`: stone machine placed, facing up

~~API and Configuration is still subject to change!~~

### Configuration

Expand All @@ -34,6 +40,9 @@ You will put [Drop Entries](docs/DROP_ENTRY.md) and Drop Resources in `drops/` a
These define types of blocks to spawn and their custom drops.
Drop chance weights and other parameters, like resistance to Fortune Enchantment or Silk Touch drop, are defined in respective Drop(/Resource) Entries.

![[Drop Actions]](image/machine_actions.png)
`drop_actions.png`: available actions in Machine's GUI

Learn more about [General Configuration](docs/CONFIG_GENERAL.md) from docs.

### Permissions
Expand Down
32 changes: 30 additions & 2 deletions docs/DROP_ENTRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ version 1
---

This document describes the structure of a Drop Entry file.
The file defines the drop rate for a specific drop.
Drop Entry is a file that defines a drop for a related resources.

> **Note:** Main configuration file for the plugin can be explored [here](docs/CONFIG_GENERAL.md).
> You must set `DropResource``DropEntry` relationship in the `config.yml` file.
Table of contents:

Expand All @@ -15,4 +16,31 @@ Table of contents:

# Drop Rates

# Custom Drops
You can set `custom_name` for custom drop title to be displayed in GUI and in chat.
Mining level required for drop is defined by `miner_level` entry. But exp from drop is defined by `miner_exp`.

Percentage chance for drop is represented by `weight`. It is a float number, not limited to 0-1 range.

Respectively, `minimal_exp` and `maximal_exp` define minimum and maximum exp orb drops.

If `ignore_fortune` is set to `true`, drop will not drop more items with `fortune` enchantment.
Same applies for `multipliable`; when set to `true`, drops won't be multiplied by server events.

# Custom Drops

Custom drop consist of `block`, `drop` and `silk` sections. All three are optional and configured the same way.
Drops are represented by Items (ItemStacks) built the following way:

```yaml
material: 'DIAMOND' # Bukkit Material
display_name: 'Diament!' # Item's name
lore: # List of lore lines
- '&7Default Drop' # ...
minimal_amount: 1 # Minimal amount of drops
maximal_amount: 1 # Minimal amount of drops
enchantments: # List of Applied Enchantments
- 'SILK_TOUCH 1' # Enchant defined by: EnchantName Level
```
- See: [Material](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html) for all available materials to use
- See: [Enchantments](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/enchantments/Enchantment.html) for all available enchantments
Binary file added image/drop_filter.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 image/machine_actions.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 image/machine_upfacing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>pl.florke.stoneage</groupId>
<artifactId>StoneAge</artifactId>
<version>RC-5</version>
<version>1.0</version>
<packaging>jar</packaging>

<name>StoneAge</name>
Expand Down Expand Up @@ -115,7 +115,7 @@
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>RELEASE</version>
<version>4.8.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
36 changes: 16 additions & 20 deletions src/main/java/pl/florke/stoneage/StoneAge.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

public final class StoneAge extends JavaPlugin {

private PluginListenerRegistry pluginListenerRegistry = new PluginListenerRegistry(this);
private final PluginListenerRegistry pluginListenerRegistry = new PluginListenerRegistry(this);
private final List<Class<? extends Listener>> listeners = new ArrayList<>(List.of(
// Stone Machines
StoneMachinePlaceListener.class, StoneMachineBreakListener.class,
Expand All @@ -59,7 +59,7 @@ public final class StoneAge extends JavaPlugin {
StatisticsIncreaseListener.class, MinerLevelUpListener.class
));

private PluginCommandsController commandExecutionController = new PluginCommandsController(this);
private final PluginCommandsController commandExecutionController = new PluginCommandsController(this);
private final Map<String, Class<? extends CommandExecutor>> commandExecutors = new HashMap<>(Map.of(
"drop", DropCommand.class,
"drophelp", DropHelpCommand.class,
Expand All @@ -77,7 +77,7 @@ public final class StoneAge extends JavaPlugin {

private Language language;
private DatabaseManager dbManager;
private MachinesConfigReader machinesConfigManager = new MachinesConfigReader(this);
private final MachinesConfigReader machinesConfigManager = new MachinesConfigReader(this);

@Override
public void onEnable() {
Expand Down Expand Up @@ -105,11 +105,10 @@ public void onEnable() {
for (final Map.Entry<String, Class<? extends CommandExecutor>> cmdExec : commandExecutors.entrySet())
commandExecutionController.registerExecutor(cmdExec.getKey(), cmdExec.getValue());

//TODO: Add this setting to the config.yml (open issue #17)
final long period = 15; // autosave period in minutes
//TODO: Add auto-save period setting to the config.yml (open issue #17)

// DBManager is initialized in reloadConfig()
getDBManager().initAsyncAutosave(period);
getDBManager().initAsyncAutosave(15);
getDropCalculator().getDropMultiplier().initMultiplierBossBar();
}

Expand Down Expand Up @@ -193,12 +192,15 @@ private void reloadDatabaseConfig(GeneralConfigReader generalConfig) {
getServer().getPluginManager().disablePlugin(this);
}

final DatabaseConfigReader databaseConfig = new DatabaseConfigReader(getConfig().getConfigurationSection("database"));
final ConfigurationSection databaseSection = getConfig().getConfigurationSection("database");
final DatabaseConfigReader databaseConfig = new DatabaseConfigReader(databaseSection);
databaseConfig.readDatabaseConnectionDetails();

// Load player statistics (& drop preferences)
dbManager = new DatabaseManager(databaseConfig);
dbManager.loadAllPlayers();

// Read previous Multiplier (before restart)
dbManager.getSQLWrapper().readPreviousMultiplierFromDatabase(dropCalculator.getDropMultiplier());
}

Expand Down Expand Up @@ -254,27 +256,21 @@ public void onDisable() {
// Plugin shutdown logic
new Message("Called plugin's onDisable() method. Bye cruel world!").log(Level.INFO);

if (getDropCalculator() != null && getDropCalculator().getDropMultiplier() != null && getDropCalculator().getDropMultiplier().getMultiplierBossBar() != null)
if (getDropCalculator() != null
&& getDropCalculator().getDropMultiplier() != null
&& getDropCalculator().getDropMultiplier().getMultiplierBossBar() != null)
getDropCalculator().getDropMultiplier().getMultiplierBossBar().removeAll();

new Message("Closing all Window Manager's GUIs... ").log(Level.INFO);
getWindowManager().closeAllWindows();

new Message("Syncing all unsaved data with the database...").log(Level.INFO);
if (playersData != null)
playersData.onDisable();
else
new Message("PlayersData not even initialized!").log(Level.WARNING);
if (playersData != null) playersData.onDisable();
else new Message("PlayersData not even initialized!").log(Level.WARNING);

new Message("Disconnecting database, closing connection pool...").log(Level.INFO);
if (dbManager != null)
dbManager.onDisable();
else
new Message("Database not even initialized!").log(Level.WARNING);
}

public List<Class<? extends Listener>> getRegisteredListeners() {
return new ArrayList<>(listeners);
if (dbManager != null) dbManager.onDisable();
else new Message("Database not even initialized!").log(Level.WARNING);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public DropEntry readDropEntry(final DropEntry.EntryType type, final NamespacedK
dropEntry.setMinerExp(minerExp);

//Needed level to drop
final int neededMinerLevel = rootSection.getInt("minimal_miner_lvl", 1);
final int neededMinerLevel = rootSection.getInt("miner_level", 1);
dropEntry.setNeededMinerLevel(neededMinerLevel);

return dropEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public class GeneralConfigReader extends ConfigSectionReader {

public GeneralConfigReader(ConfigurationSection section) {
super(section);
compile();

try {
compile();
} catch (RuntimeException ex) {
new Message("Invalid configuration!", ex.getMessage()).log(Level.SEVERE);
}
}

private void compile() {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/pl/florke/stoneage/drop/DropMultiplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package pl.florke.stoneage.drop;

import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.BarColor;
Expand All @@ -31,6 +32,8 @@
import pl.florke.stoneage.util.Message;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;

Expand Down Expand Up @@ -169,8 +172,10 @@ public void initMultiplierBossBar() {
new Message("Initialized Multiplier visualization via Boss Bar.").log(Level.INFO);

final NamespacedKey bossBarKey = new NamespacedKey(plugin, "multiplier_bossbar");
final String bossBarTitle = plugin.getLanguage("stone-multiplier-title-ticker");
multiplierBossBar = Bukkit.createBossBar(bossBarKey, Message.colors(bossBarTitle), BarColor.BLUE, BarStyle.SEGMENTED_10);
final Message bossBars = new Message(plugin.getLanguage("stone-multiplier-title-ticker"));
final List<TextComponent> bossBarTitle = new ArrayList<>(bossBars.asComponents());

multiplierBossBar = Bukkit.createBossBar(bossBarKey, bossBarTitle.getFirst().content(), BarColor.BLUE, BarStyle.SEGMENTED_10);
multiplierBossBar.setVisible(false);

final BukkitRunnable multiplierBossBarRunnable = new BukkitRunnable() {
Expand Down Expand Up @@ -236,5 +241,4 @@ private void updateBossBar(final boolean textSwitch) {
public BossBar getMultiplierBossBar() {
return multiplierBossBar;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import pl.florke.stoneage.util.Message;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class DropInfoWindow extends Window {
Expand Down Expand Up @@ -88,7 +89,6 @@ private ItemStack createIconItem(@NotNull DropEntry drop, @NotNull PlayerStats s
final float dropChance = getChancePercentage(drop);

final ItemStack icon = drop.getDropEntryIcon();
icon.setAmount(1);

final ItemMeta meta = icon.getItemMeta();

Expand Down Expand Up @@ -176,14 +176,13 @@ public void onClick(ClickType clickType, Player player, InventoryPoint clickedPo
final DropEntry dropEntry;

final int customDropsAmount = calculator.getCustomDropEntries().size();
final int resourcesAmount = calculator.getDropResourcesEntries().size();

if (clickedSlot < customDropsAmount)
dropEntry = calculator.getCustomDropEntries().get(clickedSlot);

else if (clickedSlot >= calculator.getCustomDropEntries().size())
dropEntry = List.of(calculator.getDropResourcesEntries().sequencedValues())
.get(clickedSlot - customDropsAmount).getFirst();
dropEntry = new ArrayList<>(calculator.getDropResourcesEntries().sequencedValues())
.get(clickedSlot - customDropsAmount);

else // Clicked on an empty slot, perhaps
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,18 @@ public void onStoneBreak(@NotNull BlockBreakEvent event) {
if (stoneMachine == null)
return;

event.setCancelled(true);

customizeStoneDrop(player, stoneMachine, brokenBlock);

//Cancelling default drops
event.setExpToDrop(0);
event.setDropItems(false);

new BukkitRunnable() {
@Override
public void run() {
resourceBlock.setType(Material.AIR);

resourceBlock.setType(Material.AIR);
}
}.runTaskLater(plugin, 1L);
// Respawn resource
plugin.getStoneMachine().getResourceSpawner().spawnResource(brokenBlock.getLocation());
}

private void customizeStoneDrop(@NotNull Player player, TileState machineState, Block brokenBlock) {
Expand All @@ -98,9 +97,6 @@ private void customizeStoneDrop(@NotNull Player player, TileState machineState,

player.sendBlockChange(brokenBlock.getLocation(), Material.AIR.createBlockData());

// Respawn resource
plugin.getStoneMachine().getResourceSpawner().spawnResource(brokenBlock.getLocation());

final ItemStack usedTool = player.getInventory().getItemInMainHand();
//Not applicable tool was used, means no drops
if (!player.getGameMode().isInvulnerable() && brokenBlock.getDrops(usedTool, player).isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public void onStoneMachineBreak(@NotNull BlockBreakEvent event) {
//Closing all active windows
final WindowManager windowManager = plugin.getWindowManager();
final Window brokenMachinesWindow = windowManager.getWindow(machineState);

final ItemStack stoneMachine = plugin.getStoneMachine().createStoneMachineItem();
if (gameMode != GameMode.CREATIVE)
brokenBlockLocation.getWorld().dropItemNaturally(brokenBlockLocation, stoneMachine);

if (brokenMachinesWindow == null)
return;

Expand All @@ -84,10 +89,6 @@ public void onStoneMachineBreak(@NotNull BlockBreakEvent event) {
}

}

if (gameMode != GameMode.CREATIVE) {
brokenBlockLocation.getWorld().dropItemNaturally(brokenBlockLocation, plugin.getStoneMachine().createStoneMachineItem());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public void run() {
if (!block.isEmpty())
return;

block.setType(dropResource.getBlockMaterial(), true);
block.setBlockData(dropResource.getBlockMaterial().createBlockData(), true);
block.setType(dropResource.getBlockMaterial());
block.getState().update(true);
}
}.runTaskLater(plugin, delay);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pl/florke/stoneage/machine/StoneMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public void setDropExpToFeet(boolean dropExpToFeet) {
}

public boolean isMachineDestroyTool(final ItemStack tool) {
return machineDestroyTool.isSimilar(tool.clone());
return tool.getType().equals(machineDestroyTool.getType());
}

public void setMachineDestroyTool(final ItemStack tool) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/pl/florke/stoneage/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ public static String colors(String text) {
return text.replace('&', '\u00a7');
}

@NotNull
public static TextComponent color(String text) {
if (text == null || text.isEmpty()) return Component.text("");
if (text == null || text.isEmpty())
return Component.text("");

// O.G. method
final String coloredLine = text.replace('&', '\u00a7');

return Component.text(coloredLine);
}

@NotNull
public static TextComponent color(TextComponent text) {
if (text == null) return Component.text("");
if (text == null)
return Component.text("");

// O.G. method
final String coloredLine = text.content().replace('&', '\u00a7');
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/drops/diamond.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
version: 1
minimal_miner_lvl: 5 # minimal mining level needed to drop this item from stone
minimal_tool_lvl: 1 # minimal tool's material needed to drop this item from stone {0: hand, 1: wooden+, 2: stone+/gold+, 3: iron+, 4: diamond+}
miner_level: 5 # mining level needed to drop this item from stone
miner_exp: 4 # how much plugin's mining exp is gained from it
weight: 1.0 # float value
minimal_exp: 1 # how much exp drops (at least)
Expand Down

0 comments on commit 9004d93

Please sign in to comment.