-
Notifications
You must be signed in to change notification settings - Fork 522
Support modifying data attachments using the /data command #5280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
modmuss50
wants to merge
4
commits into
FabricMC:26.1
Choose a base branch
from
modmuss50:data-command-attachments
base: 26.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...achment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/DataAccessorHandler.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.impl.attachment; | ||
|
|
||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import net.minecraft.world.level.storage.ValueInput; | ||
|
|
||
| import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget; | ||
| import net.fabricmc.fabric.api.attachment.v1.AttachmentType; | ||
|
|
||
| /** | ||
| * Replacement logic to handle applying attachments when using the /data command. | ||
| * This applies the changes using the high level APIs, ensuring that the changes are correctly synced to the client. | ||
| */ | ||
| public class DataAccessorHandler { | ||
| public static ScopedValue<Void> APPLYING_DATA_CHANGE = ScopedValue.newInstance(); | ||
|
|
||
| public static void applyDataChanges(AttachmentTarget target, ValueInput data, Runnable applyData) { | ||
| AttachmentTargetImpl targetImpl = (AttachmentTargetImpl) target; | ||
|
|
||
| Map<AttachmentType<?>, ?> oldAttachments = targetImpl.fabric_getAttachments(); | ||
| ScopedValue.where(APPLYING_DATA_CHANGE, null).run(applyData); | ||
|
|
||
| if (oldAttachments != targetImpl.fabric_getAttachments()) { | ||
| throw new AssertionError("Attachment data changed during data change application."); | ||
| } | ||
|
|
||
| IdentityHashMap<AttachmentType<?>, Object> newAttachments = AttachmentSerializingImpl.deserializeAttachmentData(data); | ||
|
|
||
| if (oldAttachments == null && newAttachments == null) { | ||
| // No attachments before or after, nothing to do | ||
| return; | ||
| } else if (oldAttachments != null && (newAttachments == null || newAttachments.isEmpty())) { | ||
| // Clear all attachments - copy keys to avoid ConcurrentModificationException | ||
| oldAttachments.keySet().stream().toList().forEach(target::removeAttached); | ||
modmuss50 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| // Update the new attachments | ||
| newAttachments.forEach((attachmentType, o) -> target.setAttached((AttachmentType) attachmentType, o)); | ||
modmuss50 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Remove all of the removed attachments - copy keys to avoid ConcurrentModificationException | ||
| if (oldAttachments != null) { | ||
| oldAttachments.keySet().stream() | ||
| .filter(attachmentType -> !newAttachments.containsKey(attachmentType)) | ||
modmuss50 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .toList() | ||
| .forEach(target::removeAttached); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
60 changes: 60 additions & 0 deletions
60
...ent-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/BlockDataAccessorMixin.java
This file contains hidden or 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,60 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.mixin.attachment; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.Unique; | ||
|
|
||
| import net.minecraft.nbt.CompoundTag; | ||
| import net.minecraft.server.commands.data.BlockDataAccessor; | ||
| import net.minecraft.server.commands.data.DataAccessor; | ||
| import net.minecraft.util.ProblemReporter; | ||
| import net.minecraft.world.level.block.entity.BlockEntity; | ||
| import net.minecraft.world.level.storage.TagValueInput; | ||
| import net.minecraft.world.level.storage.ValueInput; | ||
|
|
||
| import net.fabricmc.fabric.impl.attachment.DataAccessorHandler; | ||
|
|
||
| @Mixin(BlockDataAccessor.class) | ||
| public abstract class BlockDataAccessorMixin implements DataAccessor { | ||
| @Unique | ||
| private static final Logger LOGGER = LoggerFactory.getLogger("BlockDataAccessorMixin"); | ||
|
|
||
| @Shadow | ||
| @Final | ||
| private BlockEntity entity; | ||
|
|
||
| @WrapMethod(method = "setData") | ||
modmuss50 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public void setData(CompoundTag tag, Operation<Void> original) { | ||
| if (entity.getLevel() == null) { | ||
| // The block entity is not in a level, just follow the default logic. | ||
| original.call(tag); | ||
| return; | ||
| } | ||
|
|
||
| try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) { | ||
| ValueInput data = TagValueInput.create(reporter, this.entity.getLevel().registryAccess(), tag); | ||
| DataAccessorHandler.applyDataChanges(this.entity, data, () -> original.call(tag)); | ||
| } | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
...nt-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/EntityDataAccessorMixin.java
This file contains hidden or 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,60 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.mixin.attachment; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.Unique; | ||
|
|
||
| import net.minecraft.nbt.CompoundTag; | ||
| import net.minecraft.server.commands.data.DataAccessor; | ||
| import net.minecraft.server.commands.data.EntityDataAccessor; | ||
| import net.minecraft.util.ProblemReporter; | ||
| import net.minecraft.world.entity.Entity; | ||
| import net.minecraft.world.level.storage.TagValueInput; | ||
| import net.minecraft.world.level.storage.ValueInput; | ||
|
|
||
| import net.fabricmc.fabric.impl.attachment.DataAccessorHandler; | ||
|
|
||
| @Mixin(EntityDataAccessor.class) | ||
| public abstract class EntityDataAccessorMixin implements DataAccessor { | ||
| @Unique | ||
| private static final Logger LOGGER = LoggerFactory.getLogger("EntityDataAccessorMixin"); | ||
|
|
||
| @Shadow | ||
| @Final | ||
| private Entity entity; | ||
|
|
||
| @WrapMethod(method = "setData") | ||
| public void setData(CompoundTag tag, Operation<Void> original) { | ||
| if (entity.level() == null) { | ||
| // The block entity is not in a level, just follow the default logic. | ||
modmuss50 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| original.call(tag); | ||
| return; | ||
| } | ||
|
|
||
| try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) { | ||
| ValueInput data = TagValueInput.create(reporter, this.entity.level().registryAccess(), tag); | ||
| DataAccessorHandler.applyDataChanges(this.entity, data, () -> original.call(tag)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.