Skip to content

Commit 481e2db

Browse files
committed
commands
1 parent c35f090 commit 481e2db

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

src/main/java/cloud/grabsky/heads/HeadsListener.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,19 @@ public void onEntityDeath(final @NotNull EntityDeathEvent event) {
115115
// Adding item to the drops.
116116
event.getDrops().add(item);
117117
// Optionally, sending message to the attacker.
118-
if (event.getDamageSource().getCausingEntity() instanceof Player player)
118+
if (event.getDamageSource().getCausingEntity() instanceof Player player) {
119119
Message.of(entry.message())
120120
.placeholder("item", Component.text("[", item.getItemMeta().itemName().color()).append(item.effectiveName().hoverEvent(item.asHoverEvent())).append(Component.text("]")))
121121
.send(player);
122+
// Executing commands.
123+
if (entry.commands() != null) entry.commands().forEach(command -> {
124+
final String finalCommand = command
125+
.replace("<player>", player.getName())
126+
.replace("<entity_identifier>", (variant != null) ? (key.asString() + "/" + variant).replace("_", "-") : key.asString().replace("_", "-"));
127+
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), finalCommand);
128+
});
129+
}
130+
122131
}
123132
}
124133
}

src/main/java/cloud/grabsky/heads/object/EntityLootContainer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public final class EntityLootContainer {
5858
@Getter(AccessLevel.PUBLIC)
5959
private String message;
6060

61+
@Getter(AccessLevel.PUBLIC)
62+
private List<String> commands;
63+
6164
@Getter(AccessLevel.PUBLIC)
6265
private Map<NamespacedKey, List<EntityLootEntry>> entities;
6366

@@ -66,6 +69,7 @@ public final class EntityLootContainer {
6669
public enum Factory implements JsonAdapter.Factory {
6770
INSTANCE; // SINGLETON
6871

72+
private static final Type LIST_STRING = Types.newParameterizedType(List.class, String.class);
6973
private static final Type MAP_STRING_ENTITY_LOOT_ENTRY = Types.newParameterizedType(Map.class, NamespacedKey.class, Types.newParameterizedType(List.class, EntityLootEntry.class));
7074

7175
@Override @SuppressWarnings("unchecked")
@@ -75,6 +79,7 @@ public enum Factory implements JsonAdapter.Factory {
7579
// Getting adapters...
7680
final var ChanceAdapter = moshi.adapter(EntityLootEntry.Chance.class).nullSafe();
7781
final var MatcherAdapter = moshi.adapter(EntityLootEntry.Matcher.class).nullSafe();
82+
final var ListAdapter = moshi.adapter(LIST_STRING).nullSafe();
7883
final var MapAdapter = moshi.adapter(MAP_STRING_ENTITY_LOOT_ENTRY);
7984
// Constructing and returning the JsonAdapter for this type.
8085
return new JsonAdapter<>() {
@@ -92,6 +97,7 @@ public enum Factory implements JsonAdapter.Factory {
9297
case "default_matcher" -> result.defaultMatcher = MatcherAdapter.fromJson(in);
9398
case "permission" -> result.permission = (in.peek() != JsonReader.Token.NULL) ? in.nextString() : in.nextNull();
9499
case "message" -> result.message = (in.peek() != JsonReader.Token.NULL) ? in.nextString() : in.nextNull();
100+
case "commands" -> result.commands = (in.peek() != JsonReader.Token.NULL) ? (List<String>) ListAdapter.fromJson(in) : in.nextNull();
95101
case "entities" -> result.entities = (Map<NamespacedKey, List<EntityLootEntry>>) MapAdapter.fromJson(in);
96102
}
97103
}
@@ -118,6 +124,10 @@ else if (value.chance.perLootingLevel == null)
118124
// Setting default permission.
119125
if (value.permission == null)
120126
value.permission = result.permission;
127+
// Setting default commands.
128+
if (value.commands == null || value.commands.isEmpty() == true)
129+
if (result.commands != null && result.commands.isEmpty() == false)
130+
value.commands = result.commands;
121131
// Setting default message.
122132
if (value.message == null)
123133
value.message = result.message;

src/main/java/cloud/grabsky/heads/object/EntityLootEntry.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.squareup.moshi.JsonReader;
2121
import com.squareup.moshi.JsonWriter;
2222
import com.squareup.moshi.Moshi;
23+
import com.squareup.moshi.Types;
2324
import org.bukkit.NamespacedKey;
2425
import org.bukkit.inventory.ItemStack;
2526

@@ -57,6 +58,9 @@ public class EntityLootEntry {
5758
@Getter(AccessLevel.PUBLIC)
5859
protected @Nullable String message;
5960

61+
@Getter(AccessLevel.PUBLIC)
62+
protected List<String> commands;
63+
6064
@Getter(AccessLevel.PUBLIC)
6165
private @UnknownNullability ItemStack item;
6266

@@ -108,17 +112,22 @@ public boolean matches(final @Nullable String variant, final @Nullable Namespace
108112

109113
/* SERIALIZATION */
110114

115+
@SuppressWarnings("unchecked")
111116
public enum Factory implements JsonAdapter.Factory {
112117
INSTANCE; // SINGLETON
113118

119+
private static final Type LIST_STRING = Types.newParameterizedType(List.class, String.class);
120+
114121
@Override
115122
public @Nullable JsonAdapter<EntityLootEntry> create(final @NotNull Type type, final @NotNull Set<? extends Annotation> annotations, final @NotNull Moshi moshi) {
116123
if (EntityLootEntry.class.isAssignableFrom(getRawType(type)) == false)
117124
return null;
118125
// Getting adapters...
119126
final var ChanceAdapter = moshi.adapter(Chance.class).nullSafe();
120127
final var MatcherAdapter = moshi.adapter(Matcher.class).nullSafe();
128+
final var ListAdapter = moshi.adapter(LIST_STRING).nullSafe();
121129
final var ItemStackAdapter = moshi.adapter(ItemStack.class);
130+
122131
// Constructing and returning the JsonAdapter for this type.
123132
return new JsonAdapter<>() {
124133

@@ -136,6 +145,7 @@ public enum Factory implements JsonAdapter.Factory {
136145
case "matcher" -> result.matcher = MatcherAdapter.fromJson(in);
137146
case "permission" -> result.permission = (in.peek() != JsonReader.Token.NULL) ? in.nextString() : in.nextNull();
138147
case "message" -> result.message = (in.peek() != JsonReader.Token.NULL) ? in.nextString() : in.nextNull();
148+
case "commands" -> result.commands = (in.peek() != JsonReader.Token.NULL) ? (List<String>) ListAdapter.fromJson(in) : in.nextNull();
139149
case "item" -> result.item = ItemStackAdapter.fromJson(in);
140150
}
141151
}

src/main/resources/aggressive.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"permission": "",
1818
// Default message that will be sent to the player when they receive a head.
1919
"message": "<dark_gray>› <green>Congrats! <gray>You received <item><reset><gray> from the killed mob.",
20+
// Default list of commands to be executed when player receives a head.
21+
"commands": [],
2022
// List of entity definitions defined within this group.
2123
"entities": {
2224
"minecraft:blaze": [

src/main/resources/passive.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"permission": "",
1818
// Default message that will be sent to the player when they receive a head.
1919
"message": "<dark_gray>› <green>Congrats! <gray>You received <item><reset><gray> from the killed mob.",
20+
// Default list of commands to be executed when player receives a head.
21+
"commands": [],
2022
// List of entity definitions defined within this group.
2123
"entities": {
2224
"minecraft:allay": [

src/main/resources/player.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"permission": "",
1616
// Default message that will be sent to the player when they receive a head.
1717
"message": "<dark_gray>› <green>Congrats! <gray>You received <item><reset><gray> from the killed player.",
18+
// Default list of commands to be executed when player receives a head.
19+
"commands": [],
1820
// List of entity definitions defined within this group.
1921
"entities": {
2022
"minecraft:player": [

0 commit comments

Comments
 (0)