Skip to content
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

Closed
wants to merge 7 commits into from

Conversation

Aaron1011
Copy link
Contributor

@Aaron1011 Aaron1011 commented Nov 19, 2017

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:

  1. Redirect all locations in Vanilla/Forge where the value is set. Ideally, there will be a public setter method for you to inject into.
  2. Instead of setting the underlying field (or DataParameter, etc), call offerWithEvent on the appropriate DataHolder, putting any relevant objects into the Cause
  3. In the 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.

Copy link
Member

@gabizou gabizou left a 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();
Copy link
Member

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()));
Copy link
Member

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);
Copy link
Member

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;
Copy link
Member

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"))
Copy link
Member

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 {
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports.

@parlough
Copy link
Contributor

parlough commented Jan 20, 2018

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.

@parlough parlough closed this Jan 20, 2018
@gabizou gabizou deleted the change-value branch June 20, 2019 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants