Skip to content

Commit

Permalink
fork to 1.20.1, refactor codec wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Clickism committed Dec 23, 2024
1 parent 2690d2e commit 1155adf
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 42 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {

stonecutter {
create(rootProject) {
versions "1.21.4", "1.21.1"
versions "1.21.4", "1.21.1", "1.20.1"
vcsVersion = "1.21.4"
}
}
20 changes: 18 additions & 2 deletions src/main/java/me/clickism/clickvillagers/PartnerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@

public class PartnerState extends PersistentState {

//? if >=1.21.1 {
private static final Type<PartnerState> type = new Type<>(
PartnerState::new,
PartnerState::createFromNbt,
null
);
//?}

private final Map<UUID, Set<String>> partners = new HashMap<>();

@Override
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) {
public NbtCompound writeNbt(NbtCompound nbt
//? if >=1.21.1
, RegistryWrapper.WrapperLookup registries
) {
NbtCompound compound = new NbtCompound();
partners.forEach((uuid, set) -> {
if (set.isEmpty()) return;
Expand Down Expand Up @@ -53,7 +58,10 @@ public void removePartner(UUID uuid, String partner) {
markDirty();
}

public static PartnerState createFromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
public static PartnerState createFromNbt(NbtCompound nbt
//? if >=1.21.1
, RegistryWrapper.WrapperLookup registryLookup
) {
PartnerState state = new PartnerState();
NbtCompound compound = nbt.getCompound("TradePartnerMap");
compound.getKeys().forEach(uuid -> {
Expand All @@ -68,7 +76,15 @@ public static PartnerState getServerState(MinecraftServer server) {
ServerWorld world = server.getWorld(World.OVERWORLD);
if (world == null) throw new IllegalStateException("Overworld is null");
PersistentStateManager persistentStateManager = world.getPersistentStateManager();
//? if >=1.21.1 {
PartnerState state = persistentStateManager.getOrCreate(type, ClickVillagers.MOD_ID);
//?} else {
/*PartnerState state = persistentStateManager.getOrCreate(
PartnerState::createFromNbt,
PartnerState::new,
ClickVillagers.MOD_ID
);
*///?}
state.markDirty();
return state;
}
Expand Down
65 changes: 52 additions & 13 deletions src/main/java/me/clickism/clickvillagers/PickupHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import me.clickism.clickvillagers.util.MessageType;
import me.clickism.clickvillagers.util.Utils;
//? if >=1.21.1 {
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LoreComponent;
import net.minecraft.component.type.NbtComponent;
//?} else {


//?}
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
Expand All @@ -15,6 +20,8 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand All @@ -41,38 +48,70 @@ public void playSound(PlayerEntity player) {
};

private static final String TYPE_KEY = "EntityType";

private enum PickupVillagerType {
VILLAGER, ZOMBIE_VILLAGER
}
//? if <1.21.1
/*private static final String DATA_KEY = "ClickVillagersData";*/

public static <T extends LivingEntity & VillagerDataContainer> ItemStack toItemStack(T entity) {
NbtCompound nbt = new NbtCompound();
entity.writeNbt(nbt);
String id = EntityType.getId(entity.getType()).toString();
nbt.putString("EntityType", id);
ItemStack itemStack = getItemStack(getDisplayName(entity), NbtComponent.of(nbt));
ItemStack itemStack = getItemStack(getDisplayName(entity), nbt);
VillagerTextures.setEntityTexture(itemStack, entity);
entity.remove(Entity.RemovalReason.DISCARDED);
return itemStack;
}

private static ItemStack getItemStack(Text name, NbtComponent data) {
private static ItemStack getItemStack(Text name, NbtCompound nbt) {
ItemStack itemStack = Items.PLAYER_HEAD.getDefaultStack();
itemStack.set(DataComponentTypes.CUSTOM_DATA, data);
itemStack.set(DataComponentTypes.ITEM_NAME, name);
itemStack.set(DataComponentTypes.LORE, new LoreComponent(List.of(
writeCustomData(itemStack, nbt);
formatItem(itemStack, name, List.of(
Text.literal("Right click to place the villager back.")
.fillStyle(Style.EMPTY.withItalic(false).withColor(Formatting.DARK_GRAY)))));
.fillStyle(Style.EMPTY.withItalic(false).withColor(Formatting.DARK_GRAY)))
);
return itemStack;
}

//? if >=1.21.1 {
private static void writeCustomData(ItemStack itemStack, NbtCompound nbt) {
itemStack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(nbt));
}

@Nullable
private static NbtCompound readCustomData(ItemStack itemStack) {
NbtComponent nbt = itemStack.get(DataComponentTypes.CUSTOM_DATA);
if (nbt == null) return null;
return nbt.copyNbt();
}

private static void formatItem(ItemStack itemStack, Text name, List<Text> lore) {
itemStack.set(DataComponentTypes.ITEM_NAME, name);
itemStack.set(DataComponentTypes.LORE, new LoreComponent(lore));
}
//?} else {
/*private static void writeCustomData(ItemStack itemStack, NbtCompound nbt) {
itemStack.getOrCreateNbt().put(DATA_KEY, nbt);
}
@Nullable
private static NbtCompound readCustomData(ItemStack itemStack) {
return itemStack.getOrCreateNbt().getCompound(DATA_KEY);
}
private static void formatItem(ItemStack itemStack, Text name, List<Text> lore) {
NbtList list = new NbtList();
lore.forEach(text -> list.add(NbtString.of(Text.Serializer.toJson(text))));
NbtCompound display = itemStack.getOrCreateSubNbt("display");
display.put("Lore", list);
display.put("Name", NbtString.of(Text.Serializer.toJson(name)));
}
*///?}

@Nullable
public static Entity readEntityFromItemStack(World world, ItemStack itemStack) {
try {
NbtComponent nbtComponent = itemStack.get(DataComponentTypes.CUSTOM_DATA);
if (nbtComponent == null) return null;
NbtCompound nbt = nbtComponent.copyNbt();
NbtCompound nbt = readCustomData(itemStack);
if (nbt == null) return null;
// PickupVillagerType type = PickupVillagerType.valueOf(nbt.getString(TYPE_KEY));
String id = nbt.getString(TYPE_KEY);
if (id == null) return null;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/me/clickism/clickvillagers/VersionHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.clickism.clickvillagers;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;

public class VersionHelper {
public static void playSound(PlayerEntity player, SoundEvent soundEvent, SoundCategory category, float volume, float pitch) {
//? if >=1.21.1 {
player.playSoundToPlayer(soundEvent, category, volume, pitch);
//?} else
/*player.playSound(soundEvent, category, volume, pitch);*/
}
}
17 changes: 16 additions & 1 deletion src/main/java/me/clickism/clickvillagers/VillagerTextures.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package me.clickism.clickvillagers;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
//? if >=1.21.1 {
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ProfileComponent;
//?} else {

//?}
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.ZombieVillagerEntity;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.village.VillagerProfession;

import java.util.Map;
Expand Down Expand Up @@ -40,13 +47,21 @@ public class VillagerTextures {
public static void setEntityTexture(ItemStack itemStack, Entity entity) {
setTexture(itemStack, getTexture(entity));
}


//? if >=1.21.1 {
private static void setTexture(ItemStack itemStack, String texture) {
PropertyMap propertyMap = new PropertyMap();
propertyMap.put("textures", new Property("textures", texture));
itemStack.set(DataComponentTypes.PROFILE,
new ProfileComponent(Optional.empty(), Optional.of(UUID.randomUUID()), propertyMap));
}
//?} else {
/*private static void setTexture(ItemStack itemStack, String texture) {
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", texture));
itemStack.getOrCreateNbt().put("SkullOwner", NbtHelper.writeGameProfile(new NbtCompound(), profile));
}
*///?}

public static String getTexture(Entity entity) {
if (entity instanceof ZombieVillagerEntity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package me.clickism.clickvillagers;
package me.clickism.clickvillagers.anchor;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.passive.VillagerEntity;

public class AnchorHandler {

private static final int ANCHOR_DURATION_THRESHOLD = Integer.MAX_VALUE / 4;

public static boolean isAnchored(LivingEntity entity) {
return entity.getStatusEffects().stream().anyMatch(AnchorHandler::isAnchorEffect);
}

public static void addAnchorEffect(LivingEntity entity) {
entity.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, Integer.MAX_VALUE,
StatusEffectInstance.MAX_AMPLIFIER, true, false, false));
255, true, false, false));
}

public static void removeAnchorEffect(LivingEntity entity) {
Expand All @@ -23,6 +23,6 @@ public static void removeAnchorEffect(LivingEntity entity) {

protected static boolean isAnchorEffect(StatusEffectInstance effect) {
return effect.getEffectType().equals(StatusEffects.SLOWNESS)
&& effect.getAmplifier() == StatusEffectInstance.MAX_AMPLIFIER;
&& effect.getDuration() > ANCHOR_DURATION_THRESHOLD;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package me.clickism.clickvillagers.callback;

import me.clickism.clickvillagers.AnchorHandler;
import me.clickism.clickvillagers.PartnerState;
import me.clickism.clickvillagers.PickupHandler;
import me.clickism.clickvillagers.VillagerHandler;
import me.clickism.clickvillagers.*;
import me.clickism.clickvillagers.anchor.AnchorHandler;
import me.clickism.clickvillagers.gui.VillagerClaimGui;
import me.clickism.clickvillagers.gui.VillagerEditGui;
import me.clickism.clickvillagers.util.MessageType;
Expand Down Expand Up @@ -120,7 +118,7 @@ private void handleAnchor(PlayerEntity player, VillagerHandler<?> villagerHandle
} else {
AnchorHandler.addAnchorEffect(entity);
MessageType.CONFIRM.sendSilently(player, Text.literal("Villager anchored."));
player.playSoundToPlayer(SoundEvents.BLOCK_BEEHIVE_SHEAR, SoundCategory.MASTER, 1, 1);
VersionHelper.playSound(player, SoundEvents.BLOCK_BEEHIVE_SHEAR, SoundCategory.MASTER, 1, 1);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/me/clickism/clickvillagers/gui/BackButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class BackButton extends GuiElement {
public BackButton(GuiInterface previous) {
super(item, (index, type, action, gui) -> {
previous.open();
//? if >=1.21.1 {
previous.getPlayer().playSoundToPlayer(SoundEvents.UI_LOOM_SELECT_PATTERN, SoundCategory.MASTER, 1, 1);
//?} else
/*previous.getPlayer().playSound(SoundEvents.UI_LOOM_SELECT_PATTERN, SoundCategory.MASTER, 1, 1);*/
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
public abstract class DecoratedGui extends SimpleGui {
private static final GuiElementInterface BLACK = new GuiElementBuilder(Items.BLACK_STAINED_GLASS_PANE)
.setName(Text.literal("x").formatted(Formatting.DARK_GRAY))
//? if >=1.21.1 {
.hideDefaultTooltip()
//?} else
/*.hideFlags()*/
.build();
private static final GuiElementInterface GRAY = new GuiElementBuilder(Items.GRAY_STAINED_GLASS_PANE)
.setName(Text.literal("x").formatted(Formatting.DARK_GRAY))
//? if >=1.21.1 {
.hideDefaultTooltip()
//?} else
/*.hideFlags()*/
.build();

public DecoratedGui(ServerPlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public VillagerClaimGui(ServerPlayerEntity player, VillagerHandler<?> villagerHa
setSlot(13, new GuiElementBuilder(Items.GOLDEN_SHOVEL)
.setName(Text.literal("🔒 ").formatted(Formatting.GOLD)
.append(Text.literal("CLAIM VILLAGER").formatted(Formatting.GOLD, Formatting.BOLD)))
//? if >=1.21.1 {
.hideDefaultTooltip()
//?} else
/*.hideFlags()*/
.addLoreLine(Text.literal("Click to claim this villager.").formatted(Formatting.YELLOW))
.setCallback((index, type, action, gui) -> {
MessageType.CONFIRM.send(player, Text.literal("You claimed this villager."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public VillagerEditGui(ServerPlayerEntity player, VillagerHandler<?> villagerHan
.setName(Text.literal("🌲 ").formatted(Formatting.GOLD)
.append(Text.literal("CHANGE BIOME").formatted(Formatting.GOLD, Formatting.BOLD)))
.addLoreLine(Text.literal("Click to change the villager's biome.").formatted(Formatting.YELLOW))
.hideDefaultTooltip()
.setCallback((index, type, action, gui) -> {
MessageType.CONFIRM.playSound(player);
new VillagerBiomeChangeGui(player, villagerHandler, this).open();
Expand All @@ -48,7 +47,6 @@ public VillagerEditGui(ServerPlayerEntity player, VillagerHandler<?> villagerHan
.append(Text.literal("UNCLAIM VILLAGER").formatted(Formatting.DARK_RED, Formatting.BOLD)))
.addLoreLine(Text.literal("Click to unclaim this villager.").formatted(Formatting.RED, Formatting.BOLD))
.addLoreLine(Text.literal("Unclaimed villagers can be picked up by anyone.").formatted(Formatting.RED))
.hideDefaultTooltip()
.setCallback((index, type, action, gui) -> {
MessageType.WARN.send(player, Text.literal("You unclaimed this villager."));
villagerHandler.setOwner(null);
Expand All @@ -61,7 +59,6 @@ public VillagerEditGui(ServerPlayerEntity player, VillagerHandler<?> villagerHan
.append(Text.literal("ADD TRADING PARTNER").formatted(Formatting.WHITE, Formatting.BOLD)))
.addLoreLine(Text.literal("Click to add/remove a trading partner.").formatted(Formatting.GRAY))
.addLoreLine(Text.literal("Trading partners can trade with all of your villagers.").formatted(Formatting.GRAY))
.hideDefaultTooltip()
.setCallback((index, type, action, gui) -> {
MessageType.CONFIRM.playSound(player);
new VillagerPartnerGui(player, this).open();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.clickism.clickvillagers.gui;

import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElement;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.AnvilInputGui;
import eu.pb4.sgui.api.gui.GuiInterface;
import me.clickism.clickvillagers.PartnerState;
import me.clickism.clickvillagers.util.MessageType;
import net.minecraft.item.Items;
Expand All @@ -16,9 +14,9 @@

public class VillagerPartnerGui extends AnvilInputGui {
private final MinecraftServer server;
private final GuiInterface previous;
private final VillagerGui previous;

public VillagerPartnerGui(ServerPlayerEntity player, GuiInterface previous) {
public VillagerPartnerGui(ServerPlayerEntity player, VillagerGui previous) {
super(player, false);
this.server = player.getServer();
this.previous = previous;
Expand Down Expand Up @@ -54,7 +52,7 @@ private GuiElement getConfirmButton(String input) {
partnerState.addPartner(uuid, input);
MessageType.CONFIRM.send(player, Text.literal("Added " + input + " to your trading partners."));
}
previous.open();
new VillagerEditGui(player, previous.villagerHandler).open();
});
if (isPartner(input)) {
builder
Expand Down
Loading

0 comments on commit 1155adf

Please sign in to comment.