requires paper now, added config, added mementos, added reload command
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.youhavetrouble</groupId>
|
||||
<artifactId>BeHappyThatItHappened</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<version>1.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>BeHappyThatItHappened</name>
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
### Don't be sad it's gone, be happy that it happened.
|
||||
|
||||
Allows you to collect remains of your named companions.
|
||||
Allows you to collect remains of your named companions.
|
||||
|
||||
#### Configuration
|
||||
|
||||
You can reload the configuration with `/bhihreload` while having `bhih.reload` permission
|
||||
|
||||
```yaml
|
||||
named-entity:
|
||||
rename-drops: true # should mob drops be renamed to the name of the mob
|
||||
drop-nametag: false # should mobs drop (unusable) nametags with their name to be used as mementos
|
||||
player:
|
||||
drop-nametag: false # should players drop (unusable) nametags with their name to be used as mementos
|
||||
```
|
||||
@@ -1,12 +1,31 @@
|
||||
package me.youhavetrouble.behappythatithappened;
|
||||
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class BeHappyThatItHappened extends JavaPlugin {
|
||||
public class BeHappyThatItHappened extends JavaPlugin {
|
||||
|
||||
protected boolean renameDrops = true;
|
||||
protected boolean renamedMobsDropTag = false;
|
||||
protected boolean playersDropTag = false;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(new SadMomentListener(), this);
|
||||
reloadSadness();
|
||||
getServer().getPluginManager().registerEvents(new SadMomentListener(this), this);
|
||||
PluginCommand command = getCommand("bhihreload");
|
||||
if (command == null) {
|
||||
getLogger().warning("You messed with the plugin.yml, didn't you?");
|
||||
return;
|
||||
}
|
||||
command.setExecutor(new ReloadCommand(this));
|
||||
}
|
||||
|
||||
protected void reloadSadness() {
|
||||
saveDefaultConfig();
|
||||
renameDrops = getConfig().getBoolean("named-entity.rename-drops", true);
|
||||
renamedMobsDropTag = getConfig().getBoolean("named-entity.drop-nametag", false);
|
||||
playersDropTag = getConfig().getBoolean("player.drop-nametag", false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package me.youhavetrouble.behappythatithappened;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ReloadCommand implements TabExecutor {
|
||||
|
||||
private final BeHappyThatItHappened plugin;
|
||||
|
||||
public ReloadCommand(BeHappyThatItHappened plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
||||
plugin.reloadSadness();
|
||||
commandSender.sendMessage("Reloaded BeHappyThatItHappened config.");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,117 @@
|
||||
package me.youhavetrouble.behappythatithappened;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SadMomentListener implements Listener {
|
||||
|
||||
private final ItemMeta nametagMeta = new ItemStack(Material.NAME_TAG).getItemMeta();
|
||||
private final NamespacedKey inMemoryOfKey;
|
||||
private final BeHappyThatItHappened plugin;
|
||||
|
||||
protected SadMomentListener(BeHappyThatItHappened plugin) {
|
||||
this.plugin = plugin;
|
||||
inMemoryOfKey = new NamespacedKey(plugin, "in-memory-of");
|
||||
nametagMeta.getPersistentDataContainer().set(inMemoryOfKey, PersistentDataType.STRING, "");
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onSadMoment(EntityDeathEvent event) {
|
||||
|
||||
if (event.getEntityType().equals(EntityType.PLAYER)) return;
|
||||
String inMemoryOf = event.getEntity().getCustomName();
|
||||
Component inMemoryOf = event.getEntity().customName();
|
||||
if (inMemoryOf == null) return;
|
||||
|
||||
event.getDrops().forEach(memento -> {
|
||||
ItemMeta respect = memento.getItemMeta();
|
||||
if (respect.hasDisplayName()) return;
|
||||
respect.setDisplayName(inMemoryOf);
|
||||
memento.setItemMeta(respect);
|
||||
});
|
||||
if (plugin.renameDrops) {
|
||||
event.getDrops().forEach(memento -> {
|
||||
ItemMeta respect = memento.getItemMeta();
|
||||
if (respect.hasDisplayName()) return;
|
||||
respect.displayName(inMemoryOf);
|
||||
memento.setItemMeta(respect);
|
||||
});
|
||||
}
|
||||
|
||||
if (plugin.renamedMobsDropTag) {
|
||||
event.getDrops().add(getMemento(inMemoryOf, null));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onLossMoment(PlayerDeathEvent event) {
|
||||
if (!plugin.playersDropTag) return;
|
||||
Player player = event.getEntity();
|
||||
ItemStack memento = getMemento(player.displayName(), event.deathMessage());
|
||||
event.getDrops().add(memento);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onDesecration(PrepareAnvilEvent event) {
|
||||
ItemStack left = event.getInventory().getFirstItem();
|
||||
if (left == null) return;
|
||||
if (!Material.NAME_TAG.equals(left.getType())) return;
|
||||
ItemMeta meta = left.getItemMeta();
|
||||
PersistentDataContainer pdc = meta.getPersistentDataContainer();
|
||||
if (!pdc.has(inMemoryOfKey, PersistentDataType.STRING)) return;
|
||||
event.setResult(null);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onDesecration(PlayerInteractEntityEvent event) {
|
||||
ItemStack held = event.getPlayer().getInventory().getItemInMainHand();
|
||||
if (!Material.NAME_TAG.equals(held.getType())) return;
|
||||
ItemMeta meta = held.getItemMeta();
|
||||
PersistentDataContainer pdc = meta.getPersistentDataContainer();
|
||||
if (!pdc.has(inMemoryOfKey, PersistentDataType.STRING)) return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onDesecration(EntityDamageEvent event) {
|
||||
if (!(event.getEntityType().equals(EntityType.DROPPED_ITEM))) return;
|
||||
if (event.getCause().equals(EntityDamageEvent.DamageCause.VOID)) return;
|
||||
ItemStack item = ((org.bukkit.entity.Item) event.getEntity()).getItemStack();
|
||||
if (!Material.NAME_TAG.equals(item.getType())) return;
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
PersistentDataContainer pdc = meta.getPersistentDataContainer();
|
||||
if (!pdc.has(inMemoryOfKey, PersistentDataType.STRING)) return;
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
private ItemStack getMemento(Component name, Component message) {
|
||||
ItemStack nametag = new ItemStack(Material.NAME_TAG);
|
||||
ItemMeta meta = nametagMeta;
|
||||
meta.displayName(Component.text("In memory of ").append(name)
|
||||
.decoration(TextDecoration.ITALIC, false)
|
||||
.color(NamedTextColor.WHITE));
|
||||
if (message != null) {
|
||||
List<Component> lore = new ArrayList<>();
|
||||
lore.add(message
|
||||
.decoration(TextDecoration.ITALIC, false)
|
||||
.color(NamedTextColor.GRAY)
|
||||
);
|
||||
meta.lore(lore);
|
||||
}
|
||||
nametag.setItemMeta(meta);
|
||||
return nametag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
named-entity:
|
||||
rename-drops: true
|
||||
drop-nametag: false
|
||||
player:
|
||||
drop-nametag: false
|
||||
@@ -5,3 +5,13 @@ api-version: 1.18
|
||||
authors: [ YouHaveTrouble ]
|
||||
description: Don't be sad that they're gone, be happy they happened
|
||||
website: youhavetrouble.me
|
||||
commands:
|
||||
bhihreload:
|
||||
description: Reloads the plugin
|
||||
usage: /bhihreload
|
||||
permission: bhih.reload
|
||||
permission-message: You do not have permission to use this command
|
||||
permissions:
|
||||
bhih.reload:
|
||||
description: Allows the user to reload the plugin
|
||||
default: op
|
||||
|
||||
Reference in New Issue
Block a user