Skip to content

Commit

Permalink
Bunch of bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed May 11, 2024
1 parent 0c47528 commit d57813f
Show file tree
Hide file tree
Showing 28 changed files with 404 additions and 297 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.97.6+1.20.6

maven_group = eu.pb4

mod_version = 0.8.0-pre.1
mod_version = 0.8.0

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "eu.pb4.polymer.autohost.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"mixins": [
"ClientConnectionMixin",
"MinecraftServerMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "eu.pb4.polymer.blocks.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"plugin": "eu.pb4.polymer.blocks.mixin.PolymerBlocksMixinConfigPlugin",
"mixins": [
"polymc.PolyRegistryMixin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "eu.pb4.polymer.common.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"mixins": [
"ClientConnectionMixin",
"CommandManagerMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ default boolean handleMiningOnServer(ItemStack tool, BlockState targetBlock, Blo
default ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, @Nullable ServerPlayerEntity player) {
return PolymerItemUtils.createItemStack(itemStack, tooltipType, player);
}

default boolean shouldStorePolymerItemStackCount() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package eu.pb4.polymer.core.api.item;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.MapCodec;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.common.api.events.BooleanEvent;
Expand All @@ -24,6 +27,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.trim.ArmorTrim;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
Expand All @@ -40,20 +44,29 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

public final class PolymerItemUtils {
public static final String POLYMER_STACK = "$polymer:stack";
public static final MapCodec<ItemStack> POLYMER_STACK_CODEC = ItemStack.CODEC.fieldOf(POLYMER_STACK);
public static final MapCodec<ItemStack> POLYMER_STACK_UNCOUNTED_CODEC = ItemStack.UNCOUNTED_CODEC.fieldOf(POLYMER_STACK);
public static final MapCodec<Boolean> POLYMER_STACK_HAS_COUNT_CODEC = Codec.BOOL.optionalFieldOf("$polymer:counted", false);
public static final MapCodec<Identifier> POLYMER_STACK_ID_CODEC = Identifier.CODEC.fieldOf("id").fieldOf(POLYMER_STACK);
public static final MapCodec<Map<Identifier, NbtElement>> POLYMER_STACK_COMPONENTS_CODEC = Codec.unboundedMap(Identifier.CODEC,
Codec.PASSTHROUGH.comapFlatMap((dynamic) -> {
var nbt = dynamic.convert(NbtOps.INSTANCE).getValue();
return DataResult.success(nbt == dynamic.getValue() ? nbt.copy() : nbt);
}, (nbt) -> new Dynamic<>(NbtOps.INSTANCE, nbt.copy())))
.optionalFieldOf("components", Map.of()).fieldOf(POLYMER_STACK);
public static final Style CLEAN_STYLE = Style.EMPTY.withItalic(false).withColor(Formatting.WHITE);
/**
* Allows to force rendering of some items as polymer one (for example vanilla ones)
*/
public static final BooleanEvent<Predicate<ItemStack>> ITEM_CHECK = new BooleanEvent<>();
/**
* Allows to modify how virtual items looks before being send to client (only if using build in methods!)
* Allows to modify how virtual items looks before being sent to client (only if using build in methods!)
* It can modify virtual version directly, as long as it's returned at the end.
* You can also return new ItemStack, however please keep previous nbt so other modifications aren't removed if not needed!
*/
Expand Down Expand Up @@ -117,7 +130,6 @@ public static ItemStack getPolymerItemStack(ItemStack itemStack, RegistryWrapper
* @param tooltipContext Tooltip Context
* @param player Player being sent to
* @return Client side ItemStack
*
*/
public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipContext, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
if (getPolymerIdentifier(itemStack) != null) {
Expand Down Expand Up @@ -146,8 +158,16 @@ public static ItemStack getRealItemStack(ItemStack itemStack, RegistryWrapper.Wr

if (custom != null && custom.contains(POLYMER_STACK)) {
try {
var counted = custom.get(POLYMER_STACK_HAS_COUNT_CODEC).result().orElse(Boolean.FALSE);

//noinspection deprecation
return POLYMER_STACK_CODEC.decode(RegistryOps.of(NbtOps.INSTANCE, lookup), NbtOps.INSTANCE.getMap(custom.getNbt()).getOrThrow()).getOrThrow();
var x = (counted ? POLYMER_STACK_CODEC : POLYMER_STACK_UNCOUNTED_CODEC).decode(RegistryOps.of(NbtOps.INSTANCE, lookup), NbtOps.INSTANCE.getMap(custom.getNbt()).getOrThrow()).getOrThrow();

if (!counted) {
x.setCount(itemStack.getCount());
}

return x;
} catch (Throwable ignored) {

}
Expand All @@ -163,6 +183,7 @@ public static ItemStack getRealItemStack(ItemStack itemStack, RegistryWrapper.Wr
public static Identifier getPolymerIdentifier(ItemStack itemStack) {
return getPolymerIdentifier(itemStack.get(DataComponentTypes.CUSTOM_DATA));
}

public static Identifier getPolymerIdentifier(@Nullable NbtComponent custom) {

if (custom != null && custom.contains(POLYMER_STACK)) {
Expand All @@ -189,6 +210,31 @@ public static Identifier getServerIdentifier(@Nullable NbtComponent nbtData) {
return getPolymerIdentifier(nbtData);
}

@Nullable
public static Map<Identifier, NbtElement> getServerComponents(ItemStack stack) {
return getPolymerComponents(stack.get(DataComponentTypes.CUSTOM_DATA));
}

@Nullable
public static Map<Identifier, NbtElement> getPolymerComponents(ItemStack stack) {
return getPolymerComponents(stack.get(DataComponentTypes.CUSTOM_DATA));

}

@Nullable
public static Map<Identifier, NbtElement> getServerComponents(@Nullable NbtComponent nbtData) {
return getPolymerComponents(nbtData);
}

@Nullable
public static Map<Identifier, NbtElement> getPolymerComponents(@Nullable NbtComponent nbtData) {
if (nbtData == null || getPolymerIdentifier(nbtData) == null) {
return null;
}

return nbtData.get(POLYMER_STACK_COMPONENTS_CODEC).result().orElse(Map.of());
}

public static boolean isPolymerServerItem(ItemStack itemStack) {
return isPolymerServerItem(itemStack, PolymerUtils.getPlayerContext());
}
Expand Down Expand Up @@ -318,6 +364,7 @@ public static int getSafeColor(int inputColor) {
* @param player Player seeing it
* @return Client side ItemStack
*/

public static ItemStack createItemStack(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return createItemStack(itemStack, PolymerUtils.getTooltipType(player), player);
}
Expand All @@ -339,11 +386,15 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
Item item = itemStack.getItem();
int cmd = -1;
int color = -1;
boolean storeCount;
if (itemStack.getItem() instanceof PolymerItem virtualItem) {
var data = PolymerItemUtils.getItemSafely(virtualItem, itemStack, player);
item = data.item();
cmd = data.customModelData();
color = data.color();
storeCount = virtualItem.shouldStorePolymerItemStackCount();
} else {
storeCount = false;
}

ItemStack out = new ItemStack(item, itemStack.getCount());
Expand All @@ -367,9 +418,15 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
}
try {
PolymerCommonUtils.executeWithPlayerContext(null, () -> {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
var comp = NbtComponent.of(
(NbtCompound) (storeCount ? POLYMER_STACK_CODEC : POLYMER_STACK_UNCOUNTED_CODEC).encoder()
.encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
);
if (storeCount) {
out.set(DataComponentTypes.CUSTOM_DATA, comp.with(POLYMER_STACK_HAS_COUNT_CODEC, true).getOrThrow());
} else {
out.set(DataComponentTypes.CUSTOM_DATA, comp);
}
});
} catch (Throwable e) {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(POLYMER_STACK_ID_CODEC, Registries.ITEM.getId(itemStack.getItem())).getOrThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.item.TooltipType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -252,16 +254,17 @@ public static void pickBlock(ServerPlayerEntity player, BlockPos pos, boolean wi
return;
}

BlockEntity blockEntity = null;
if (isCreative && withNbt && blockState.hasBlockEntity()) {
blockEntity = player.getWorld().getBlockEntity(pos);
var blockEntity = player.getWorld().getBlockEntity(pos);
if (blockEntity != null && (!blockEntity.copyItemDataRequiresOperator() || player.isCreativeLevelTwoOp())) {
itemStack.applyComponentsFrom(blockEntity.createComponentMap());
itemStack.set(DataComponentTypes.BLOCK_ENTITY_DATA, NbtComponent.of(blockEntity.createComponentlessNbt(player.getRegistryManager())));
}
}


PlayerInventory playerInventory = player.getInventory();
if (blockEntity != null) {
addBlockEntityNbt(itemStack, blockEntity);
}


int i = playerInventory.getSlotWithStack(itemStack);
if (isCreative) {
Expand All @@ -279,25 +282,6 @@ public static void pickBlock(ServerPlayerEntity player, BlockPos pos, boolean wi
}
}


private static void addBlockEntityNbt(ItemStack stack, BlockEntity blockEntity) {
// todo

/*NbtCompound nbtCompound = blockEntity.createNbtWithId(blockEntity.getWorld().getRegistryManager());
NbtCompound nbtCompound3;
if (stack.getItem() instanceof PlayerHeadItem && nbtCompound.contains("SkullOwner")) {
nbtCompound3 = nbtCompound.getCompound("SkullOwner");
stack.getOrCreateNbt().put("SkullOwner", nbtCompound3);
} else {
stack.setSubNbt("BlockEntityTag", nbtCompound);
nbtCompound3 = new NbtCompound();
NbtList nbtList = new NbtList();
nbtList.add(NbtString.of("\"(+NBT)\""));
nbtCompound3.put("Lore", nbtList);
stack.setSubNbt("display", nbtCompound3);
}*/
}

public static void pickEntity(ServerPlayerEntity player, Entity entity) {
var isCreative = player.isCreative();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package eu.pb4.polymer.core.impl.client.compat;

import eu.pb4.polymer.core.api.client.PolymerClientUtils;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.client.InternalClientRegistry;
import eu.pb4.polymer.core.impl.client.interfaces.ClientItemGroupExtension;
import eu.pb4.polymer.networking.impl.client.ClientPacketRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.*;
Expand All @@ -17,6 +13,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

Expand All @@ -34,29 +31,14 @@ public static boolean areEqualItems(ItemStack a, ItemStack b) {
if (!areSamePolymerType(a, b)) {
return false;
}
var nbtA = getBackingNbt(a);
var nbtB = getBackingNbt(b);
var nbtA = getBackingComponents(a);
var nbtB = getBackingComponents(b);
return Objects.equals(nbtA, nbtB);
}

@Nullable
public static NbtCompound getBackingNbt(ItemStack stack) {
if (!stack.contains(DataComponentTypes.CUSTOM_DATA)) {
return null;
}
var nbt = stack.get(DataComponentTypes.CUSTOM_DATA).getNbt();
if (PolymerItemUtils.getServerIdentifier(stack) == null) {
return nbt;
}

var maybeNbt = nbt.getCompound(PolymerItemUtils.POLYMER_STACK).getCompound("components");

if (maybeNbt != null) {
return maybeNbt;
}
maybeNbt = nbt.getCompound("PolyMcOriginal");

return maybeNbt != null && maybeNbt.contains("tag", NbtElement.COMPOUND_TYPE) ? maybeNbt.getCompound("tag") : null;
public static Map<Identifier, NbtElement> getBackingComponents(ItemStack stack) {
return PolymerItemUtils.getServerComponents(stack);
}

public static boolean isServerSide(ItemStack stack) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.polymer.core.impl.networking;

import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.common.impl.entity.InternalEntityHelpers;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
import eu.pb4.polymer.core.api.other.PolymerStatusEffect;
Expand Down Expand Up @@ -93,6 +94,10 @@ public static boolean prevent(ServerCommonNetworkHandler handler, Packet<?> pack
return true;
} else if ((packet instanceof EntityEquipmentUpdateS2CPacket original && original.getEquipmentList().isEmpty()) || !EntityAttachedPacket.shouldSend(packet, player)) {
return true;
} else if ((packet instanceof EntityAttributesS2CPacket original
&& EntityAttachedPacket.get(packet, original.getEntityId()) instanceof PolymerEntity entity
&& InternalEntityHelpers.isLivingEntity(entity.getPolymerEntityType(player)))) {
return true;
} else if (packet instanceof BlockEntityUpdateS2CPacket be && PolymerBlockUtils.isPolymerBlockEntityType(be.getBlockEntityType())) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

// todo
@ApiStatus.Internal
public record PolymerItemEntry(int numId, Identifier identifier, ItemStack representation) {
public static final PacketCodec<ContextByteBuf, PolymerItemEntry> CODEC = PacketCodec.of(PolymerItemEntry::write, PolymerItemEntry::read);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public static void playSound(ServerPlayerEntity player, RegistryEntry<SoundEvent
playSound(player, soundEvent.value());
}
public static void playSound(ServerPlayerEntity player, SoundEvent soundEvent) {
// todo
//player.playSound(soundEvent, SoundCategory.MASTER, 0.2f, 1);
player.playSoundToPlayer(soundEvent, SoundCategory.MASTER, 0.2f, 1);
}

@FunctionalInterface
Expand Down
Loading

0 comments on commit d57813f

Please sign in to comment.