fix: i did it wrong

version 1.0.1
This commit is contained in:
trafficlunar 2026-06-30 22:07:36 +01:00
parent 28a4e1d5c4
commit 6df0e5ab15
3 changed files with 67 additions and 7 deletions

View file

@ -6,7 +6,7 @@
<groupId>net.trafficlunar</groupId> <groupId>net.trafficlunar</groupId>
<artifactId>VillagerRehab</artifactId> <artifactId>VillagerRehab</artifactId>
<version>1.0.0</version> <version>1.0.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>VillagerRehab</name> <name>VillagerRehab</name>

View file

@ -0,0 +1,40 @@
package net.trafficlunar.villagerrehab;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType;
// Stolen from https://github.com/spartacus04/InstantRestock/blob/master/src/main/java/me/spartacus04/instantrestock/TradesDataType.kt
public class TradesDataType implements PersistentDataType<byte[], int[]> {
@Override
public Class<byte[]> getPrimitiveType() {
return byte[].class;
}
@Override
public Class<int[]> getComplexType() {
return int[].class;
}
@Override
public byte[] toPrimitive(int[] complex, PersistentDataAdapterContext context) {
byte[] bytes = new byte[complex.length];
for (int i = 0; i < complex.length; i++) {
bytes[i] = (byte) complex[i];
}
return bytes;
}
@Override
public int[] fromPrimitive(byte[] primitive, PersistentDataAdapterContext context) {
int[] ints = new int[primitive.length];
for (int i = 0; i < primitive.length; i++) {
ints[i] = primitive[i];
}
return ints;
}
}

View file

@ -4,22 +4,24 @@ import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.trafficlunar.villagerrehab.commands.WandCommand; import net.trafficlunar.villagerrehab.TradesDataType;
import org.bukkit.Bukkit; import org.bukkit.NamespacedKey;
import org.bukkit.Effect;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.MerchantRecipe;
import java.util.List;
import static net.trafficlunar.villagerrehab.VillagerRehab.wandItem; import static net.trafficlunar.villagerrehab.VillagerRehab.wandItem;
public class InteractListener implements Listener { public class InteractListener implements Listener {
private final NamespacedKey KEY = new NamespacedKey("instantrestock", "instant_restock");
@EventHandler @EventHandler
public void onInteract(PlayerInteractEntityEvent event) { public void onInteract(PlayerInteractEntityEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -30,7 +32,25 @@ public class InteractListener implements Listener {
if (clickedEntity instanceof Villager villager) { if (clickedEntity instanceof Villager villager) {
event.setCancelled(true); event.setCancelled(true);
villager.clearReputations();
int[] originalUses = villager.getPersistentDataContainer().get(KEY, new TradesDataType());
int i = 0;
List<MerchantRecipe> recipes = villager.getRecipes();
for (MerchantRecipe recipe : recipes) {
if (originalUses != null && i < originalUses.length) {
recipe.setMaxUses(originalUses[i]);
} else {
recipe.setMaxUses(12); // fallback
}
recipe.setUses(0);
recipe.setDemand(0);
i++;
}
villager.setRecipes(recipes);
player.sendMessage(Component.text("Rehabbed!").color(NamedTextColor.GOLD)); player.sendMessage(Component.text("Rehabbed!").color(NamedTextColor.GOLD));
player.playSound(Sound.sound(Key.key("block.amethyst_block.chime"), Sound.Source.BLOCK, 100.0f, 1.0f)); player.playSound(Sound.sound(Key.key("block.amethyst_block.chime"), Sound.Source.BLOCK, 100.0f, 1.0f));
player.getWorld().spawnParticle(Particle.FIREWORK, villager.getLocation(), 150); player.getWorld().spawnParticle(Particle.FIREWORK, villager.getLocation(), 150);