-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for server side entity attributes, fix support for polyme…
…r things used in enchantment definitions
- Loading branch information
Showing
23 changed files
with
366 additions
and
44 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
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
62 changes: 62 additions & 0 deletions
62
polymer-core/src/main/java/eu/pb4/polymer/core/api/other/PolymerMapCodec.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,62 @@ | ||
package eu.pb4.polymer.core.api.other; | ||
|
||
import com.mojang.serialization.*; | ||
import eu.pb4.polymer.common.api.PolymerCommonUtils; | ||
import eu.pb4.polymer.core.api.utils.PolymerObject; | ||
import eu.pb4.polymer.core.api.utils.PolymerUtils; | ||
import net.minecraft.enchantment.EnchantmentLevelBasedValue; | ||
import net.minecraft.enchantment.effect.AllOfEnchantmentEffects; | ||
import net.minecraft.enchantment.effect.EnchantmentEntityEffect; | ||
import net.minecraft.enchantment.effect.EnchantmentLocationBasedEffect; | ||
import net.minecraft.enchantment.effect.EnchantmentValueEffect; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
public class PolymerMapCodec<T> extends MapCodec<T> implements PolymerObject { | ||
private final MapCodec<T> selfCodec; | ||
private final MapCodec<Object> fallbackCodec; | ||
private final Object fallbackValue; | ||
|
||
public <K> PolymerMapCodec(MapCodec<T> selfCodec, MapCodec<K> fallbackCodec, K fallbackValue) { | ||
this.selfCodec = selfCodec; | ||
//noinspection unchecked | ||
this.fallbackCodec = (MapCodec<Object>) fallbackCodec; | ||
this.fallbackValue = fallbackValue; | ||
} | ||
|
||
public static <T extends EnchantmentValueEffect> MapCodec<T> ofEnchantmentValueEffect(MapCodec<T> codec) { | ||
return new PolymerMapCodec<T>(codec, AllOfEnchantmentEffects.ValueEffects.CODEC, new AllOfEnchantmentEffects.ValueEffects(List.of())); | ||
} | ||
|
||
public static <T extends EnchantmentLocationBasedEffect> MapCodec<T> ofEnchantmentLocationBasedEffect(MapCodec<T> codec) { | ||
return new PolymerMapCodec<T>(codec, AllOfEnchantmentEffects.LocationBasedEffects.CODEC, new AllOfEnchantmentEffects.LocationBasedEffects(List.of())); | ||
} | ||
|
||
public static <T extends EnchantmentEntityEffect> MapCodec<T> ofEnchantmentEntityEffect(MapCodec<T> codec) { | ||
return new PolymerMapCodec<T>(codec, AllOfEnchantmentEffects.EntityEffects.CODEC, new AllOfEnchantmentEffects.EntityEffects(List.of())); | ||
} | ||
|
||
public static <T extends EnchantmentLevelBasedValue> MapCodec<T> ofEnchantmentLevelBasedValue(MapCodec<T> codec) { | ||
return new PolymerMapCodec<T>(codec, EnchantmentLevelBasedValue.Constant.TYPE_CODEC, new EnchantmentLevelBasedValue.Constant(0)); | ||
} | ||
|
||
@Override | ||
public <T1> Stream<T1> keys(DynamicOps<T1> ops) { | ||
return this.selfCodec.keys(ops); | ||
} | ||
|
||
@Override | ||
public <T1> DataResult<T> decode(DynamicOps<T1> ops, MapLike<T1> input) { | ||
return this.selfCodec.decode(ops, input); | ||
} | ||
|
||
@Override | ||
public <T1> RecordBuilder<T1> encode(T input, DynamicOps<T1> ops, RecordBuilder<T1> prefix) { | ||
if (PolymerCommonUtils.isServerNetworkingThread()) { | ||
return this.fallbackCodec.encode(this.fallbackValue, ops, prefix); | ||
} | ||
|
||
return this.selfCodec.encode(input, ops, prefix); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
polymer-core/src/main/java/eu/pb4/polymer/core/impl/PolymericObject.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,5 @@ | ||
package eu.pb4.polymer.core.impl; | ||
|
||
public interface PolymericObject { | ||
boolean polymer$isPolymeric(); | ||
} |
3 changes: 1 addition & 2 deletions
3
.../core/impl/TransformingDataComponent.java → ...ymer/core/impl/TransformingComponent.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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package eu.pb4.polymer.core.impl; | ||
|
||
import net.minecraft.component.ComponentType; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
|
||
public interface TransformingDataComponent { | ||
public interface TransformingComponent { | ||
Object polymer$getTransformed(ServerPlayerEntity player); | ||
boolean polymer$requireModification(ServerPlayerEntity player); | ||
} |
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
33 changes: 33 additions & 0 deletions
33
polymer-core/src/main/java/eu/pb4/polymer/core/mixin/entity/EntityAttributeMixin.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,33 @@ | ||
package eu.pb4.polymer.core.mixin.entity; | ||
|
||
import com.llamalad7.mixinextras.injector.ModifyExpressionValue; | ||
import com.mojang.serialization.Codec; | ||
import eu.pb4.polymer.common.api.PolymerCommonUtils; | ||
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils; | ||
import eu.pb4.polymer.core.api.item.PolymerItemUtils; | ||
import eu.pb4.polymer.core.impl.PolymerImplUtils; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.attribute.EntityAttributes; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
import net.minecraft.text.HoverEvent; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.text.Texts; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import xyz.nucleoid.packettweaker.PacketContext; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
@Mixin(EntityAttribute.class) | ||
public abstract class EntityAttributeMixin { | ||
@ModifyExpressionValue(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/registry/Registry;getEntryCodec()Lcom/mojang/serialization/Codec;")) | ||
private static Codec<RegistryEntry<EntityAttribute>> patchCodec(Codec<RegistryEntry<EntityAttribute>> codec) { | ||
return codec.xmap(Function.identity(), content -> { // Encode | ||
if (PolymerCommonUtils.isServerNetworkingThread() && PolymerEntityUtils.isPolymerEntityAttribute(content)) { | ||
return EntityAttributes.ZOMBIE_SPAWN_REINFORCEMENTS; | ||
} | ||
return content; | ||
}); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
.../main/java/eu/pb4/polymer/core/mixin/item/component/AttributeModifiersComponentMixin.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,44 @@ | ||
package eu.pb4.polymer.core.mixin.item.component; | ||
|
||
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils; | ||
import eu.pb4.polymer.core.impl.TransformingComponent; | ||
import net.minecraft.component.type.AttributeModifiersComponent; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Mixin(AttributeModifiersComponent.class) | ||
public class AttributeModifiersComponentMixin implements TransformingComponent { | ||
@Shadow @Final private List<AttributeModifiersComponent.Entry> modifiers; | ||
|
||
@Shadow @Final private boolean showInTooltip; | ||
|
||
@Override | ||
public Object polymer$getTransformed(ServerPlayerEntity player) { | ||
if (!polymer$requireModification(player)) { | ||
return this; | ||
} | ||
var list = new ArrayList<AttributeModifiersComponent.Entry>(); | ||
for (var entry : this.modifiers) { | ||
if (!PolymerEntityUtils.isPolymerEntityAttribute(entry.attribute())) { | ||
list.add(entry); | ||
} | ||
} | ||
|
||
return new AttributeModifiersComponent(list, this.showInTooltip); | ||
} | ||
|
||
@Override | ||
public boolean polymer$requireModification(ServerPlayerEntity player) { | ||
for (var x : this.modifiers) { | ||
if (PolymerEntityUtils.isPolymerEntityAttribute(x.attribute())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.