-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
225bae3
commit c931a4a
Showing
17 changed files
with
382 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# 3.2.2 | ||
# 3.2.3 | ||
|
||
- Fix class loading error (again) | ||
- Implement Attachments on ItemStacks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
fabric/src/main/java/earth/terrarium/botarium/fabric/data/DataUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package earth.terrarium.botarium.fabric.data; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.fabricmc.fabric.api.attachment.v1.AttachmentType; | ||
import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtOps; | ||
import net.minecraft.nbt.Tag; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
@SuppressWarnings({"UnstableApiUsage", "unchecked"}) | ||
public class DataUtils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger("botarium-item-data-attachment-impl"); | ||
|
||
public static <H extends AttachmentTargetImpl> void copyAttachments(H from, H to, Predicate<AttachmentType<?>> filter) { | ||
Map<AttachmentType<?>, ?> attachments = from.fabric_getAttachments(); | ||
if (attachments == null) return; | ||
for (var entry : attachments.entrySet()) { | ||
AttachmentType<Object> type = (AttachmentType<Object>) entry.getKey(); | ||
var serializer = type.persistenceCodec(); | ||
if (serializer != null && filter.test(type)) { | ||
Tag tag = write(type, entry.getValue()); | ||
Object data = read(type, tag); | ||
to.setAttached(type, data); | ||
} | ||
} | ||
} | ||
|
||
public static <H extends AttachmentTargetImpl> boolean areAttachmentsCompatible(H first, H second) { | ||
Map<AttachmentType<?>, ?> firstAttachments = first.fabric_getAttachments(); | ||
Map<AttachmentType<?>, ?> secondAttachments = second.fabric_getAttachments(); | ||
if (firstAttachments == null && secondAttachments == null) return true; | ||
if (secondAttachments == null || firstAttachments == null) return false; | ||
for (var entry : firstAttachments.entrySet()) { | ||
AttachmentType<Object> type = (AttachmentType<Object>) entry.getKey(); | ||
if (type.persistenceCodec() != null) { | ||
var otherData = secondAttachments.get(type); | ||
Supplier<Object> initializer = type.initializer(); | ||
if (otherData == null && initializer != null) | ||
otherData = initializer.get(); | ||
if (!Objects.equals(write(type, entry.getValue()), write(type, otherData))) | ||
return false; | ||
} | ||
} | ||
for (var entry : secondAttachments.entrySet()) { | ||
AttachmentType<Object> type = (AttachmentType<Object>) entry.getKey(); | ||
if (type.persistenceCodec() != null) { | ||
var data = firstAttachments.get(type); | ||
if (data != null) | ||
continue; // already checked in the first loop | ||
data = type.initializer().get(); | ||
if(!Objects.equals(write(type, entry.getValue()), write(type, data))) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static <T> Tag write(AttachmentType<T> type, T value) { | ||
AtomicReference<Tag> tag = new AtomicReference<>(new CompoundTag()); | ||
Codec<T> codec = type.persistenceCodec(); | ||
if (codec == null) return tag.get(); | ||
codec.encodeStart(NbtOps.INSTANCE, value) | ||
.get() | ||
.ifRight(partial -> { | ||
LOGGER.warn("Couldn't serialize attachment " + type.identifier() + ", skipping. Error:"); | ||
LOGGER.warn(partial.message()); | ||
}) | ||
.ifLeft(tag::set); | ||
return tag.get(); | ||
} | ||
|
||
public static <T> T read(AttachmentType<T> type, Tag tag) { | ||
AtomicReference<T> value = new AtomicReference<>(null); | ||
Codec<T> codec = type.persistenceCodec(); | ||
if (codec == null) return value.get(); | ||
codec.decode(NbtOps.INSTANCE, tag) | ||
.get() | ||
.ifRight(partial -> { | ||
LOGGER.warn("Couldn't deserialize attachment " + type.identifier() + ", skipping. Error:"); | ||
LOGGER.warn(partial.message()); | ||
}) | ||
.ifLeft(parsed -> value.set(parsed.getFirst())); | ||
|
||
return value.get(); | ||
} | ||
|
||
public static <T> T copy(AttachmentType<T> type, T value) { | ||
return read(type, write(type, value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
fabric/src/main/java/earth/terrarium/botarium/fabric/fluid/holder/MutableItemVariant.java
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
fabric/src/main/java/earth/terrarium/botarium/mixin/ItemAttachmentHolderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package earth.terrarium.botarium.mixin; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.fabricmc.fabric.api.attachment.v1.AttachmentType; | ||
import net.fabricmc.fabric.impl.attachment.AttachmentSerializingImpl; | ||
import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl; | ||
import net.fabricmc.fabric.impl.transfer.item.ItemVariantImpl; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
@Mixin({ItemStack.class, ItemVariantImpl.class}) | ||
public class ItemAttachmentHolderMixin implements AttachmentTargetImpl { | ||
@Unique | ||
@Nullable | ||
private IdentityHashMap<AttachmentType<?>, Object> fabric_dataAttachments = null; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
@Nullable | ||
public <T> T getAttached(AttachmentType<T> type) { | ||
return fabric_dataAttachments == null ? null : (T) fabric_dataAttachments.get(type); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
@Nullable | ||
public <T> T setAttached(AttachmentType<T> type, @Nullable T value) { | ||
// Extremely inelegant, but the only alternative is separating out these two mixins and duplicating code | ||
if (value == null) { | ||
if (fabric_dataAttachments == null) { | ||
return null; | ||
} | ||
|
||
T removed = (T) fabric_dataAttachments.remove(type); | ||
|
||
if (fabric_dataAttachments.isEmpty()) { | ||
fabric_dataAttachments = null; | ||
} | ||
|
||
return removed; | ||
} else { | ||
if (fabric_dataAttachments == null) { | ||
fabric_dataAttachments = new IdentityHashMap<>(); | ||
} | ||
|
||
return (T) fabric_dataAttachments.put(type, value); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasAttached(AttachmentType<?> type) { | ||
return fabric_dataAttachments != null && fabric_dataAttachments.containsKey(type); | ||
} | ||
|
||
@Override | ||
public void fabric_writeAttachmentsToNbt(CompoundTag nbt) { | ||
AttachmentSerializingImpl.serializeAttachmentData(nbt, fabric_dataAttachments); | ||
} | ||
|
||
@Override | ||
public void fabric_readAttachmentsFromNbt(CompoundTag nbt) { | ||
fabric_dataAttachments = AttachmentSerializingImpl.deserializeAttachmentData(nbt); | ||
} | ||
|
||
@Override | ||
public boolean fabric_hasPersistentAttachments() { | ||
return AttachmentSerializingImpl.hasPersistentAttachments(fabric_dataAttachments); | ||
} | ||
|
||
@Override | ||
public Map<AttachmentType<?>, ?> fabric_getAttachments() { | ||
return fabric_dataAttachments; | ||
} | ||
|
||
} |
Oops, something went wrong.