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
[WIP] Refactor and implement ChangeDataHolderEvent.ValueChange #1605
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small changes, otherwise looks good. Aside from API discussions we've had about removing GetKey annotation.
@@ -122,6 +124,11 @@ public boolean supports(Class<? extends DataManipulator<?, ?>> holderClass) { | |||
.orElseGet(DataTransactionResult::failNoData); | |||
} | |||
|
|||
@Override | |||
public <E> Optional<ChangeDataHolderEvent.ValueChange> offerWithEvent(Key<? extends BaseValue<E>> key, E value, Cause cause) { | |||
throw new UnsupportedOperationException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message maybe?
@Override | ||
public <R> Optional<R> getDefault(Key<? extends BaseValue<R>> key) { | ||
return DataUtil.getNbtProcessor(this.getDataType(), key) | ||
.flatMap(processor -> processor.readFrom(this.data).map(v -> v.getDefault())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multi-line the nested mapping. In the event there's an exception, we want to be able to pinpoint which lambda is having an issue.
@@ -97,6 +98,8 @@ | |||
*/ | |||
DataTransactionResult set(DataHolder dataHolder, M manipulator, MergeFunction function); | |||
|
|||
//void setWithEvent(DataHolder container, M manipulator, Cause cause); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intended to be used later maybe? comments welcome.
import org.spongepowered.api.data.DataTransactionResult; | ||
import org.spongepowered.api.data.key.Key; | ||
import org.spongepowered.api.data.manipulator.DataManipulator; | ||
import org.spongepowered.api.data.manipulator.ImmutableDataManipulator; | ||
import org.spongepowered.api.data.value.BaseValue; | ||
import org.spongepowered.api.data.value.ValueContainer; | ||
import org.spongepowered.api.data.value.immutable.ImmutableValue; | ||
import org.spongepowered.api.event.cause.Cause; | ||
import org.spongepowered.api.event.data.ChangeDataHolderEvent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imports?
this.player = (Player) player; | ||
} | ||
|
||
@Inject(method = "addStats(Lnet/minecraft/item/ItemFood;Lnet/minecraft/item/ItemStack;)V", at = @At(value = "HEAD")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a redirect if you're modifying the cause stack, this way you can wrap it in a try with autocloseable frames.
import java.util.Optional; | ||
|
||
@Plugin(id = "food-change-test", authors = "Aaron1011") | ||
public class FoodChangeTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is going to constantly print to the player, should make it a toggle to join in (reduces the amount of commenting when debugging implementation).
@@ -25,6 +25,8 @@ | |||
package org.spongepowered.test; | |||
|
|||
import org.slf4j.Logger; | |||
import org.spongepowered.api.data.key.Keys; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imports.
Going to close this for now due to SpongePowered/SpongeAPI#1728 and #1670 which provide a method of implementing and listening to the event. If you'd like to make other changes, open a new PR with those in relation to what is now in the API and implementation. |
SpongeAPI | SpongeCommon | SpongeForge
See the SpongeAPI PR for more details.
This is the basic pattern for firing a
ChangeDataHolderEvent.ValueChange
event for a given key:DataParameter, etc
), callofferWithEvent
on the appropriateDataHolder
, putting any relevant objects into theCause
ValueProcessor
for the key in question, bypass the setter you redirected before, if applicable. This ensures that setting the value does not end up recursing infinitely between your redirected setter and the value processor.Basically, the idea is to ensure that all changes done by mods/vanilla/forge end up firing an event, while API changes (e.g. anything going through the value processor) does not. The decision as to whether or not to fire an event for a plugin change is handled on the API side, and is not the implementation's concern.