Skip to content

Commit 360a368

Browse files
committed
Refactor HeadDrops to core module, and add 1.21.11 new mob heads
1 parent f2c656d commit 360a368

File tree

13 files changed

+253
-364
lines changed

13 files changed

+253
-364
lines changed

craftbook-bukkit/doctools/src/main/java/org/enginehub/craftbook/internal/util/CommandUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import com.sk89q.worldedit.util.formatting.text.Component;
2121
import com.sk89q.worldedit.util.formatting.text.TextComponent;
2222
import org.enginehub.craftbook.PlatformCommandManager;
23+
import org.enginehub.craftbook.bukkit.mechanics.headdrops.HeadDropsCommands;
2324
import org.enginehub.craftbook.mechanic.MechanicCommandRegistrar;
2425
import org.enginehub.craftbook.mechanics.area.clipboard.AreaCommands;
25-
import org.enginehub.craftbook.mechanics.headdrops.HeadDropsCommands;
2626
import org.enginehub.craftbook.mechanics.signcopier.SignEditCommands;
2727
import org.enginehub.piston.Command;
2828
import org.enginehub.piston.config.TextConfig;

craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanic/BukkitMechanicManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void setup() {
195195
.id("head_drops")
196196
.name("HeadDrops")
197197
.description(TranslatableComponent.of("craftbook.headdrops.description"))
198-
.className("org.enginehub.craftbook.mechanics.headdrops.HeadDrops")
198+
.className("org.enginehub.craftbook.bukkit.mechanics.headdrops.BukkitHeadDrops")
199199
.category(MechanicCategory.GENERAL)
200200
.buildAndRegister();
201201

craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanic/MechanicTypes.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.enginehub.craftbook.mechanics.area.Bridge;
2121
import org.enginehub.craftbook.mechanics.area.Door;
2222
import org.enginehub.craftbook.mechanics.area.Gate;
23-
import org.enginehub.craftbook.mechanics.headdrops.HeadDrops;
2423
import org.enginehub.craftbook.mechanics.minecart.MinecartCollisionEntry;
2524
import org.enginehub.craftbook.mechanics.minecart.MinecartEmptyDecay;
2625
import org.enginehub.craftbook.mechanics.minecart.MinecartExitRemover;
@@ -53,7 +52,6 @@ public class MechanicTypes {
5352
public static final @Nullable MechanicType<Bridge> BRIDGE = get("bridge");
5453
public static final @Nullable MechanicType<Door> DOOR = get("door");
5554
public static final @Nullable MechanicType<Gate> GATE = get("gate");
56-
public static final @Nullable MechanicType<HeadDrops> HEAD_DROPS = get("head_drops");
5755
public static final @Nullable MechanicType<CartBooster> MINECART_BOOSTER = get("minecart_booster");
5856
public static final @Nullable MechanicType<MinecartCollisionEntry> MINECART_COLLISION_ENTRY = get("minecart_collision_entry");
5957
public static final @Nullable MechanicType<CartDispenser> MINECART_DISPENSER = get("minecart_dispenser");

craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanics/betterai/BukkitBetterAI.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,14 @@ public void onEntityShootBow(EntityShootBowEvent event) {
155155
}
156156

157157
if (isEntityEnabled(event.getEntity(), criticalBow)) {
158-
int amount;
159-
switch (event.getEntity().getWorld().getDifficulty()) {
160-
case EASY:
161-
amount = 100;
162-
break;
163-
case HARD:
164-
amount = 20;
165-
break;
166-
case NORMAL:
167-
amount = 50;
168-
break;
169-
case PEACEFUL:
170-
default:
171-
return;
172-
}
173-
174-
if (ThreadLocalRandom.current().nextInt(amount) == 0) {
158+
int amount = switch (event.getEntity().getWorld().getDifficulty()) {
159+
case EASY -> 100;
160+
case HARD -> 20;
161+
case NORMAL -> 50;
162+
case PEACEFUL -> 0;
163+
};
164+
165+
if (amount > 0 && ThreadLocalRandom.current().nextInt(amount) == 0) {
175166
CraftBookPlugin.logDebugMessage("Performing critical hit.", "ai-mechanics.shoot-bow.critical");
176167
event.getEntity().getWorld().spawnParticle(Particle.CRIT, event.getEntity().getEyeLocation(), 10);
177168
event.getProjectile().setFireTicks(5000);

craftbook-bukkit/src/main/java/org/enginehub/craftbook/mechanics/headdrops/HeadDrops.java renamed to craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanics/headdrops/BukkitHeadDrops.java

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
* see <http://www.gnu.org/licenses/>.
1414
*/
1515

16-
package org.enginehub.craftbook.mechanics.headdrops;
16+
package org.enginehub.craftbook.bukkit.mechanics.headdrops;
1717

1818
import com.destroystokyo.paper.profile.PlayerProfile;
19+
import com.destroystokyo.paper.profile.ProfileProperty;
1920
import com.google.common.collect.Maps;
2021
import com.sk89q.util.yaml.YAMLProcessor;
22+
import com.sk89q.worldedit.bukkit.BukkitAdapter;
2123
import com.sk89q.worldedit.util.formatting.text.TextComponent;
2224
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
2325
import net.kyori.adventure.text.Component;
2426
import net.kyori.adventure.text.format.Style;
2527
import net.kyori.adventure.text.format.TextDecoration;
28+
import org.bukkit.Bukkit;
2629
import org.bukkit.GameMode;
2730
import org.bukkit.Material;
2831
import org.bukkit.NamespacedKey;
@@ -45,37 +48,54 @@
4548
import org.bukkit.inventory.meta.ItemMeta;
4649
import org.bukkit.inventory.meta.SkullMeta;
4750
import org.bukkit.persistence.PersistentDataType;
48-
import org.enginehub.craftbook.AbstractCraftBookMechanic;
4951
import org.enginehub.craftbook.CraftBook;
5052
import org.enginehub.craftbook.CraftBookPlayer;
5153
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
5254
import org.enginehub.craftbook.mechanic.CraftBookMechanic;
5355
import org.enginehub.craftbook.mechanic.MechanicCommandRegistrar;
5456
import org.enginehub.craftbook.mechanic.MechanicType;
57+
import org.enginehub.craftbook.mechanics.headdrops.HeadDrops;
58+
import org.enginehub.craftbook.mechanics.headdrops.SkinData;
5559
import org.enginehub.craftbook.util.EventUtil;
5660
import org.jspecify.annotations.Nullable;
5761

5862
import java.util.HashMap;
5963
import java.util.List;
6064
import java.util.Map;
6165
import java.util.Objects;
66+
import java.util.Set;
67+
import java.util.UUID;
6268
import java.util.concurrent.ThreadLocalRandom;
69+
import java.util.stream.Collectors;
6370

64-
public class HeadDrops extends AbstractCraftBookMechanic implements Listener {
71+
public class BukkitHeadDrops extends HeadDrops implements Listener {
6572

6673
private static final Map<EntityType, PlayerProfile> TEXTURE_MAP = Maps.newHashMap();
74+
protected static final String HEAD_NAME = "cb-headdrops";
75+
private static final UUID DEFAULT_UUID = UUID.fromString("a233eb4b-4cab-42cd-9fd9-7e7b9a3f74be");
76+
77+
protected static PlayerProfile createProfile(String texture) {
78+
PlayerProfile profile = Bukkit.createProfile(DEFAULT_UUID, HEAD_NAME);
79+
profile.setProperty(new ProfileProperty("textures", texture));
80+
81+
return profile;
82+
}
6783

6884
static {
6985
// skip if the CRAFTBOOK_DOCGEN environment variable is set
7086
// This breaks docgen currently.
7187
if (System.getenv("CRAFTBOOK_DOCGEN") == null) {
72-
SkinData.addDefaultSkinData(TEXTURE_MAP);
88+
SkinData.addDefaultSkinData((entityType, textureString) -> {
89+
if (entityType != null) {
90+
TEXTURE_MAP.put(BukkitAdapter.adapt(entityType), createProfile(textureString));
91+
}
92+
});
7393
}
7494
}
7595

7696
private final NamespacedKey headDropsEntityKey = new NamespacedKey("craftbook", "head_drops_entity");
7797

78-
public HeadDrops(MechanicType<? extends CraftBookMechanic> mechanicType) {
98+
public BukkitHeadDrops(MechanicType<? extends CraftBookMechanic> mechanicType) {
7999
super(mechanicType);
80100
}
81101

@@ -204,7 +224,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
204224
}
205225
} else {
206226
PlayerProfile profile = skull.getPlayerProfile();
207-
if (profile == null || profile.getName() == null || profile.getName().equals(SkinData.HEAD_NAME)) {
227+
if (profile == null || profile.getName() == null || profile.getName().equals(HEAD_NAME)) {
208228
return;
209229
}
210230

@@ -319,38 +339,12 @@ private NamespacedKey parseKey(String name) {
319339
}
320340
}
321341

322-
private boolean enableMobs;
323-
private boolean enablePlayers;
324-
private boolean playerKillsOnly;
325-
private boolean overrideNatural;
326-
private double dropRate;
327-
private double lootingModifier;
328-
private boolean nameOnClick;
329342
private HashMap<NamespacedKey, Double> customDropRates;
330343
private HashMap<NamespacedKey, PlayerProfile> customSkins;
331344

332345
@Override
333346
public void loadFromConfiguration(YAMLProcessor config) {
334-
config.setComment("drop-mob-heads", "Whether mobs should drop their heads when killed.");
335-
enableMobs = config.getBoolean("drop-mob-heads", true);
336-
337-
config.setComment("drop-player-heads", "Whether players should drop their heads when killed.");
338-
enablePlayers = config.getBoolean("drop-player-heads", true);
339-
340-
config.setComment("require-player-killer", "Only drop heads when killed by a player. (Allows requiring permission)");
341-
playerKillsOnly = config.getBoolean("require-player-killer", true);
342-
343-
config.setComment("override-natural-head-drops", "Override natural head drops, this will cause natural head drops to use the chances provided by CraftBook. (Eg, Wither Skeleton Heads)");
344-
overrideNatural = config.getBoolean("override-natural-head-drops", false);
345-
346-
config.setComment("drop-rate", "A value between 1 and 0 which dictates the global chance of heads being dropped. This can be overridden per-entity type.");
347-
dropRate = config.getDouble("drop-rate", 0.05);
348-
349-
config.setComment("looting-rate-modifier", "This amount is added to the chance for every looting level on an item. Eg, a chance of 0.05(5%) and a looting mod of 0.05(5%) on a looting 3 sword, would give a 0.20 chance (20%).");
350-
lootingModifier = config.getDouble("looting-rate-modifier", 0.05);
351-
352-
config.setComment("show-name-right-click", "When enabled, right clicking a placed head will say the owner of the head.");
353-
nameOnClick = config.getBoolean("show-name-right-click", true);
347+
super.loadFromConfiguration(config);
354348

355349
config.setComment("drop-rates", "A list of custom drop rates for different mobs");
356350
customDropRates = new HashMap<>();
@@ -366,10 +360,31 @@ public void loadFromConfiguration(YAMLProcessor config) {
366360
customSkins = new HashMap<>();
367361
if (config.getKeys("custom-skins") != null) {
368362
for (String key : config.getKeys("custom-skins")) {
369-
customSkins.put(parseKey(key), SkinData.createProfile(config.getString("custom-skins." + key)));
363+
customSkins.put(parseKey(key), createProfile(config.getString("custom-skins." + key)));
370364
}
371365
} else {
372366
config.addNode("custom-skins");
373367
}
374368
}
369+
370+
private static final Set<EntityType> IGNORED_ENTITIES = Set.of(
371+
EntityType.PLAYER, EntityType.ZOMBIE, EntityType.CREEPER,
372+
EntityType.SKELETON, EntityType.WITHER_SKELETON,
373+
EntityType.ARMOR_STAND, EntityType.ENDER_DRAGON, EntityType.PIGLIN,
374+
EntityType.UNKNOWN, EntityType.MANNEQUIN
375+
);
376+
377+
@SuppressWarnings("unused")
378+
private static void printMissingSkins() {
379+
String missingText = Registry.ENTITY_TYPE.stream()
380+
.filter(type -> !IGNORED_ENTITIES.contains(type) && type.isAlive())
381+
.filter(type -> !TEXTURE_MAP.containsKey(type))
382+
.map(EntityType::getKey)
383+
.map(NamespacedKey::toString)
384+
.collect(Collectors.joining(", "));
385+
386+
if (!missingText.isEmpty()) {
387+
CraftBook.LOGGER.warn(missingText);
388+
}
389+
}
375390
}

craftbook-bukkit/src/main/java/org/enginehub/craftbook/mechanics/headdrops/HeadDropsCommands.java renamed to craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanics/headdrops/HeadDropsCommands.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* see <http://www.gnu.org/licenses/>.
1414
*/
1515

16-
package org.enginehub.craftbook.mechanics.headdrops;
16+
package org.enginehub.craftbook.bukkit.mechanics.headdrops;
1717

1818
import com.sk89q.worldedit.bukkit.BukkitAdapter;
1919
import com.sk89q.worldedit.command.util.CommandPermissions;
@@ -41,17 +41,17 @@
4141
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
4242
public class HeadDropsCommands {
4343

44-
private final HeadDrops headDrops;
44+
private final BukkitHeadDrops headDrops;
4545

46-
public static void register(CommandManager commandManager, CommandRegistrationHandler registration, HeadDrops headDrops) {
46+
public static void register(CommandManager commandManager, CommandRegistrationHandler registration, BukkitHeadDrops headDrops) {
4747
registration.register(
4848
commandManager,
4949
HeadDropsCommandsRegistration.builder(),
5050
new HeadDropsCommands(headDrops)
5151
);
5252
}
5353

54-
private HeadDropsCommands(HeadDrops headDrops) {
54+
private HeadDropsCommands(BukkitHeadDrops headDrops) {
5555
this.headDrops = headDrops;
5656
}
5757

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/>
3+
*
4+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5+
* License as published by the Free
6+
* Software Foundation, either version 3 of the License, or (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9+
* warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License along with this program. If not,
13+
* see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
@org.jspecify.annotations.NullMarked
17+
package org.enginehub.craftbook.bukkit.mechanics.headdrops;

craftbook-bukkit/src/main/java/org/enginehub/craftbook/bukkit/mechanics/piston/BukkitBetterPistons.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ public void onSignChange(SignChangeEvent event) {
9393

9494
PistonType type = null;
9595

96+
String line1 = PlainTextComponentSerializer.plainText().serialize(event.line(1));
9697
for (PistonType testType : PistonType.values()) {
97-
if (isEnabled(testType) && event.getLine(1).equalsIgnoreCase(testType.getSignText())) {
98+
if (isEnabled(testType) && line1.equalsIgnoreCase(testType.getSignText())) {
9899
event.line(1, Component.text(testType.getSignText()));
99100
type = testType;
100101
break;

0 commit comments

Comments
 (0)