Refactoring
This commit is contained in:
parent
b7bf79ee31
commit
c1a1978e44
6 changed files with 122 additions and 101 deletions
11
README.md
11
README.md
|
@ -17,4 +17,13 @@ openssl x509 -inform pem -in cert.cer -pubkey -noout > public_key.pem
|
|||
When using `mvn`, override with `-Djarsigner.`
|
||||
```
|
||||
mvn clean package -Djarsigner.keystore=/home/user/mykeystore.jks -Djarsigner.alias=mykey
|
||||
```
|
||||
```
|
||||
|
||||
### Color scheme
|
||||
The following color scheme is used for chat messages:
|
||||
- Errors: RED
|
||||
- "Soft errors" (like no permission): GRAY
|
||||
- Status messages: GRAY
|
||||
- Notice / call for action: YELLOW (optionally also BOLD)
|
||||
- Information: GOLD
|
||||
- Highlight: AQUA (TODO do that)
|
|
@ -1,25 +1,34 @@
|
|||
package eu.m724.giants;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Drop {
|
||||
public final ItemStack itemStack;
|
||||
public final int min, max;
|
||||
public final double chance;
|
||||
|
||||
public Drop(ItemStack itemStack, int min, int max, double chance) {
|
||||
this.itemStack = itemStack;
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.chance = chance;
|
||||
}
|
||||
|
||||
public ItemStack generateItemStack() {
|
||||
public record Drop(ItemStack itemStack, int min, int max, double chance) {
|
||||
/**
|
||||
* Randomizes quantity and returns {@link ItemStack}.<br>
|
||||
* This should be called every drop.
|
||||
*
|
||||
* @return A {@link ItemStack} with randomized quantity
|
||||
*/
|
||||
private ItemStack generate() {
|
||||
int amount = ThreadLocalRandom.current().nextInt(min, max + 1);
|
||||
|
||||
ItemStack itemStack = this.itemStack.clone();
|
||||
itemStack.setAmount(amount);
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the item at {@code location} taking into account quantity and chance.
|
||||
*
|
||||
* @param location The location to drop the drop at
|
||||
*/
|
||||
public void dropAt(Location location) {
|
||||
if (chance > ThreadLocalRandom.current().nextDouble()) {
|
||||
ItemStack itemStack = generate();
|
||||
location.getWorld().dropItemNaturally(location, itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import org.bukkit.event.Listener;
|
|||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.entity.EntitySpawnEvent;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
@ -30,7 +29,6 @@ public class GiantProcessor implements Listener {
|
|||
final Configuration configuration;
|
||||
private final Logger logger;
|
||||
|
||||
// private final Set<Giant> trackedGiants = new HashSet<>();
|
||||
final Set<Husk> trackedHusks = new HashSet<>();
|
||||
final Map<Entity, Location> giantLocationMap = new HashMap<>();
|
||||
|
||||
|
@ -85,7 +83,6 @@ public class GiantProcessor implements Listener {
|
|||
|
||||
if (giant instanceof Giant) {
|
||||
trackedHusks.add(husk);
|
||||
//trackedGiants.add((Giant) giant);
|
||||
|
||||
logger.fine("Tracking a loaded Giant at " + giant.getLocation());
|
||||
} else {
|
||||
|
@ -148,14 +145,9 @@ public class GiantProcessor implements Listener {
|
|||
logger.fine("A Giant died at " + location);
|
||||
|
||||
for (Drop drop : configuration.drops) {
|
||||
logger.fine("Rolling a drop");
|
||||
logger.fine("Rolling a drop: " + drop.itemStack().toString());
|
||||
|
||||
if (drop.chance > random.nextDouble()) {
|
||||
ItemStack is = drop.generateItemStack();
|
||||
entity.getWorld().dropItemNaturally(location, is);
|
||||
|
||||
logger.fine("Dropped " + is);
|
||||
}
|
||||
drop.dropAt(location);
|
||||
}
|
||||
|
||||
for (Entity passenger : entity.getPassengers()) {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package eu.m724.giants;
|
||||
|
||||
import eu.m724.giants.updater.UpdateCommand;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
@ -30,64 +32,78 @@ public class GiantsCommand implements CommandExecutor {
|
|||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
sender.sendMessage("Giants " + plugin.getDescription().getVersion());
|
||||
sender.sendMessage(ChatColor.GRAY + "Giants " + plugin.getDescription().getVersion());
|
||||
return true;
|
||||
}
|
||||
|
||||
String action = args[0];
|
||||
|
||||
if (!sender.hasPermission("giants.command." + action)) {
|
||||
sender.sendMessage("You don't have permission to use this command, or it doesn't exist.");
|
||||
sender.sendMessage(ChatColor.GRAY + "You don't have permission to use this command, or it doesn't exist.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = sender instanceof Player ? (Player) sender : null;
|
||||
|
||||
if (action.equals("serialize")) {
|
||||
if (player != null) {
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("chance", 1);
|
||||
map.put("quantityMin", itemStack.getAmount());
|
||||
map.put("quantityMax", itemStack.getAmount());
|
||||
map.put("itemStack", itemStack);
|
||||
list.add(map);
|
||||
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
Method method = yamlConfiguration.getClass().getMethod("setInlineComments", String.class, List.class);
|
||||
|
||||
yamlConfiguration.set("v", list);
|
||||
method.invoke(yamlConfiguration, "v", List.of("Copy the below content to your config.yml"));
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
yamlConfiguration.set("v", null); // two latter exceptions happen after setting
|
||||
yamlConfiguration.set("copy_everything_below_to_config_yml", list);
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
String name = "item-" + now + ".yml";
|
||||
File file = new File(plugin.getDataFolder(), name);
|
||||
|
||||
try {
|
||||
yamlConfiguration.save(file);
|
||||
sender.sendMessage("Saved to plugins/Giants/" + name + ". To add it as a drop, see instructions in the file.");
|
||||
} catch (IOException e) {
|
||||
sender.sendMessage("Error saving file. See console for details.");
|
||||
throw new RuntimeException("Error saving file to " + file.getName(), e);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("Only players can use this command.");
|
||||
}
|
||||
serializeCommand(sender);
|
||||
} else if (action.equals("update")) {
|
||||
if (updateCommand != null)
|
||||
updateCommand.onCommand(sender, command, label, args);
|
||||
updateCommand.updateCommand(sender, args);
|
||||
else
|
||||
sender.sendMessage("Updater is disabled");
|
||||
sender.sendMessage(ChatColor.GRAY + "Updater is disabled");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void serializeCommand(CommandSender sender) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage("Only players can use this command.");
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (itemStack.getType() == Material.AIR) {
|
||||
sender.sendMessage(ChatColor.RED + "You must hold an item in your main hand.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String name = serializeDrop(player.getInventory().getItemInMainHand());
|
||||
sender.sendMessage("Saved to plugins/Giants/" + name + ". Please follow the instructions in that file.");
|
||||
} catch (IOException e) {
|
||||
sender.sendMessage("Error saving file. See console for details.");
|
||||
throw new RuntimeException("Error saving drop configuration file", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String serializeDrop(ItemStack itemStack) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("chance", 1);
|
||||
map.put("quantityMin", itemStack.getAmount());
|
||||
map.put("quantityMax", itemStack.getAmount());
|
||||
map.put("itemStack", itemStack);
|
||||
list.add(map);
|
||||
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
Method method = yamlConfiguration.getClass().getMethod("setInlineComments", String.class, List.class);
|
||||
|
||||
yamlConfiguration.set("v", list);
|
||||
method.invoke(yamlConfiguration, "v", List.of("Copy the below content to your config.yml"));
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
yamlConfiguration.set("v", null); // two latter exceptions happen after setting
|
||||
yamlConfiguration.set("copy_everything_below_to_config_yml", list);
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
String name = "item-" + now + ".yml";
|
||||
File file = new File(plugin.getDataFolder(), name);
|
||||
|
||||
yamlConfiguration.save(file);
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import eu.m724.giants.updater.PluginUpdater;
|
|||
import eu.m724.giants.updater.UpdateCommand;
|
||||
import eu.m724.jarupdater.verify.VerificationException;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bstats.charts.SimplePie;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
@ -56,9 +57,13 @@ public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
|
|||
|
||||
UpdateCommand updateCommand = new UpdateCommand(updater);
|
||||
|
||||
getCommand("giants").setExecutor(new GiantsCommand(this, configuration, updateCommand));
|
||||
getCommand("giants").setExecutor(new GiantsCommand(this, updateCommand));
|
||||
|
||||
new Metrics(this, 14131);
|
||||
Metrics metrics = new Metrics(this, 14131);
|
||||
metrics.addCustomChart(new SimplePie("jump_mode", () -> String.valueOf(configuration.jumpMode)));
|
||||
metrics.addCustomChart(new SimplePie("jump_condition", () -> String.valueOf(configuration.jumpCondition)));
|
||||
metrics.addCustomChart(new SimplePie("jump_delay", () -> String.valueOf(configuration.jumpDelay)));
|
||||
metrics.addCustomChart(new SimplePie("jump_height", () -> String.valueOf(configuration.jumpHeight)));
|
||||
}
|
||||
|
||||
// TODO api, untested
|
||||
|
|
|
@ -4,7 +4,6 @@ import net.md_5.bungee.api.ChatColor;
|
|||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -37,80 +36,71 @@ public class UpdateCommand {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (updater == null) {
|
||||
sender.sendMessage("Updater is disabled");
|
||||
return true;
|
||||
}
|
||||
|
||||
sender.sendMessage("Please wait...");
|
||||
sender.sendMessage("Channel: " + updater.getEnvironment().getChannel());
|
||||
public void updateCommand(CommandSender sender, String[] args) {
|
||||
sender.sendMessage(ChatColor.GRAY + "Please wait...");
|
||||
sender.sendMessage(ChatColor.GRAY + "Channel: " + updater.getEnvironment().getChannel());
|
||||
|
||||
if (updater.updatePending) {
|
||||
sender.sendMessage("Server restart required");
|
||||
sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "(!) Server restart required");
|
||||
}
|
||||
|
||||
if (args.length == 1) { // remember this function is proxied
|
||||
String action = args.length > 1 ? args[1] : null; // remember this function is proxied
|
||||
|
||||
if (action == null) {
|
||||
updater.getLatestVersion().thenAccept(metadata -> {
|
||||
updater.getCurrentVersion().thenAccept(metadata2 -> {
|
||||
sender.sendMessage("You're on Giants " + metadata2.getLabel() + " released " + formatDate(metadata2.getTimestamp()));
|
||||
sender.sendMessage(ChatColor.GOLD + "You're on Giants " + metadata2.getLabel() + " released " + formatDate(metadata2.getTimestamp()));
|
||||
sendChangelogMessage(sender, metadata2.getChangelogUrl());
|
||||
}).exceptionally(e -> {
|
||||
sender.sendMessage("Error retrieving information about current version, see console for details. " + e.getMessage());
|
||||
sender.sendMessage(ChatColor.RED + "Error retrieving information about current version, see console for details. " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
});
|
||||
|
||||
if (metadata != null) {
|
||||
sender.sendMessage("An update is available!");
|
||||
sender.sendMessage("Giants " + metadata.getLabel() + " released " + formatDate(metadata.getTimestamp()));
|
||||
sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "An update is available!");
|
||||
sender.sendMessage(ChatColor.GOLD + "Giants " + metadata.getLabel() + " released " + formatDate(metadata.getTimestamp()));
|
||||
sendChangelogMessage(sender, metadata.getChangelogUrl());
|
||||
sender.sendMessage("To download: /giants update download");
|
||||
sender.sendMessage(ChatColor.GOLD + "To download: /giants update download");
|
||||
} else {
|
||||
sender.sendMessage("No new updates");
|
||||
sender.sendMessage(ChatColor.GRAY + "No new updates");
|
||||
}
|
||||
}).exceptionally(e -> {
|
||||
sender.sendMessage("Error checking for update. See console for details.");
|
||||
sender.sendMessage(ChatColor.RED + "Error checking for update. See console for details.");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
String action = args[1]; // remember this function is proxied
|
||||
|
||||
if (!sender.hasPermission("giants.update." + action)) {
|
||||
sender.sendMessage("You don't have permission to use this command, or it doesn't exist.");
|
||||
return true;
|
||||
sender.sendMessage(ChatColor.GRAY + "You don't have permission to use this command, or it doesn't exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (action.equals("download")) {
|
||||
sender.sendMessage("Started download");
|
||||
sender.sendMessage(ChatColor.GRAY + "Started download");
|
||||
|
||||
updater.downloadLatestVersion().thenAccept(file -> {
|
||||
sender.sendMessage("Download finished, install with /giants update install");
|
||||
sender.sendMessage(ChatColor.GREEN + "Download finished, install with /giants update install"); // TODO make this clickable
|
||||
}).exceptionally(e -> {
|
||||
sender.sendMessage("Download failed. See console for details.");
|
||||
sender.sendMessage(ChatColor.RED + "Download failed. See console for details.");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
});
|
||||
} else if (action.equals("install")) {
|
||||
try {
|
||||
updater.installLatestVersion().thenAccept(v -> {
|
||||
sender.sendMessage("Installation completed, restart server to apply.");
|
||||
sender.sendMessage(ChatColor.GREEN + "Installation completed, restart server to apply.");
|
||||
updater.updatePending = true;
|
||||
}).exceptionally(e -> {
|
||||
sender.sendMessage("Install failed, see console for details. " + e.getMessage());
|
||||
sender.sendMessage(ChatColor.RED + "Install failed, see console for details. " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
});
|
||||
} catch (NoSuchFileException e) {
|
||||
sender.sendMessage("First, download the update: /giants update download");
|
||||
sender.sendMessage(ChatColor.YELLOW + "Download the update first: /giants update download");
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private String formatDate(long timestamp) {
|
||||
|
|
Loading…
Add table
Reference in a new issue