-
Notifications
You must be signed in to change notification settings - Fork 55
feat: creating compatibility layer #371
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
Draft
Farenheith
wants to merge
8
commits into
alRex-U:main
Choose a base branch
from
Farenheith:compatibility-layer
base: main
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.
Draft
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f026ee9
feat: creating compatibility layer
Farenheith 53dc4c6
feat: wrapping Vector3d
Farenheith d98673b
refactor: removing LazyOptional
Farenheith 9dcff20
refactor: adding LevelWrapper
Farenheith e4ca62d
refactor: adding BlockEntity, Containers and InteractionResult wrappers
Farenheith a27382c
refactor: fixing WeakCache loophole
Farenheith ab476e5
refactor: moving compatibility out of api
Farenheith c36736d
refactor: adding more compatibilty leayer elements
Farenheith 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
Empty file.
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/alrex/parcool/api/compatibility/AbstractClientPlayerWrapper.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,26 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import net.minecraft.client.entity.player.AbstractClientPlayerEntity; | ||
| import net.minecraftforge.event.TickEvent.PlayerTickEvent; | ||
|
|
||
| public class AbstractClientPlayerWrapper extends PlayerWrapper { | ||
| private AbstractClientPlayerEntity player; | ||
| private static final WeakCache<AbstractClientPlayerEntity, AbstractClientPlayerWrapper> cache = new WeakCache<>(); | ||
|
|
||
| protected AbstractClientPlayerWrapper(AbstractClientPlayerEntity player) { | ||
| super(player); | ||
| this.player = player; | ||
| } | ||
|
|
||
| public static AbstractClientPlayerWrapper get(AbstractClientPlayerEntity player) { | ||
| return cache.get(player, () -> new AbstractClientPlayerWrapper(player)); | ||
| } | ||
|
|
||
| public static AbstractClientPlayerWrapper get(PlayerTickEvent event) { | ||
| return get((AbstractClientPlayerEntity)event.player); | ||
| } | ||
|
|
||
| public AbstractClientPlayerEntity getInstance() { | ||
| return this.player; | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
src/main/java/com/alrex/parcool/api/compatibility/AxisWrapper.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,52 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import net.minecraft.util.math.vector.Quaternion; | ||
| import net.minecraft.util.math.vector.Vector3f; | ||
|
|
||
| public final class AxisWrapper { | ||
| public static AxisWrapper XN = new AxisWrapper(Vector3f.XN); | ||
| public static AxisWrapper XP = new AxisWrapper(Vector3f.XP); | ||
| public static AxisWrapper YN = new AxisWrapper(Vector3f.YN); | ||
| public static AxisWrapper YP = new AxisWrapper(Vector3f.YP); | ||
| public static AxisWrapper ZN = new AxisWrapper(Vector3f.ZN); | ||
| public static AxisWrapper ZP = new AxisWrapper(Vector3f.ZP); | ||
| private Vector3f vector; | ||
|
|
||
| public static AxisWrapper createXZ(Vec3Wrapper vec) { | ||
| return new AxisWrapper(vec.x(), 0, vec.z()); | ||
| } | ||
|
|
||
| public AxisWrapper(Vector3f vec) { | ||
| vector = vec; | ||
| } | ||
| public AxisWrapper(float x, float y, float z) { | ||
| vector = new Vector3f(x, y, z); | ||
| } | ||
| public AxisWrapper(double x, double y, double z) { | ||
| vector = new Vector3f((float)x, (float)y, (float)z); | ||
| } | ||
|
|
||
| public Quaternion rotationDegrees(float angleDegree) { | ||
| return vector.rotationDegrees(angleDegree); | ||
| } | ||
|
|
||
| public Quaternion rotation(float angle) { | ||
| return vector.rotation(angle); | ||
| } | ||
|
|
||
| public static AxisWrapper fromVector(Vec3Wrapper midPointD) { | ||
| return new AxisWrapper(midPointD.x(), midPointD.y(), midPointD.z()); | ||
| } | ||
|
|
||
| public float x() { | ||
| return vector.x(); | ||
| } | ||
|
|
||
| public float y() { | ||
| return vector.y(); | ||
| } | ||
|
|
||
| public float z() { | ||
| return vector.z(); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/alrex/parcool/api/compatibility/BlockEntityWrapper.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,16 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import net.minecraft.tileentity.TileEntity; | ||
|
|
||
| public class BlockEntityWrapper { | ||
| private TileEntity blockEntity; | ||
|
|
||
| public BlockEntityWrapper(TileEntity blockEntity) { | ||
| this.blockEntity = blockEntity; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public <T extends TileEntity> T cast(Class<T> classTarget) { | ||
| return classTarget.isInstance(blockEntity) ? (T) blockEntity : null; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/alrex/parcool/api/compatibility/ChannelInstanceWrapper.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,11 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import com.alrex.parcool.ParCool; | ||
| import com.alrex.parcool.common.network.StartBreakfallMessage; | ||
| import net.minecraftforge.fml.network.PacketDistributor; | ||
|
|
||
| public class ChannelInstanceWrapper { | ||
| public static void send(ServerPlayerWrapper player, StartBreakfallMessage message) { | ||
| ParCool.CHANNEL_INSTANCE.send(PacketDistributor.PLAYER.with(() -> player.getInstance()), message); | ||
| } | ||
| } |
96 changes: 96 additions & 0 deletions
96
src/main/java/com/alrex/parcool/api/compatibility/ClientPlayerWrapper.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,96 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.entity.player.AbstractClientPlayerEntity; | ||
| import net.minecraft.client.entity.player.ClientPlayerEntity; | ||
| import net.minecraft.entity.LivingEntity; | ||
| import net.minecraft.entity.player.PlayerEntity; | ||
|
|
||
| public class ClientPlayerWrapper extends AbstractClientPlayerWrapper { | ||
| private static final Minecraft mc = Minecraft.getInstance(); | ||
| private ClientPlayerEntity player; | ||
| private static final WeakCache<ClientPlayerEntity, ClientPlayerWrapper> cache = new WeakCache<>(); | ||
|
|
||
| public ClientPlayerWrapper(ClientPlayerEntity player) { | ||
| super(player); | ||
| this.player = player; | ||
| } | ||
|
|
||
| // All static get methods grouped together | ||
| public static ClientPlayerWrapper get(ClientPlayerEntity player) { | ||
| return cache.get(player, () -> new ClientPlayerWrapper(player)); | ||
| } | ||
|
|
||
| public static ClientPlayerWrapper get(PlayerEntity player) { | ||
| return get((ClientPlayerEntity)player); | ||
| } | ||
|
|
||
| public static ClientPlayerWrapper get(PlayerWrapper player) { | ||
| ClientPlayerEntity clientPlayer = (ClientPlayerEntity) player.getInstance(); | ||
| return cache.get(clientPlayer, () -> new ClientPlayerWrapper(clientPlayer)); | ||
| } | ||
|
|
||
| @Nullable | ||
| public static ClientPlayerWrapper get() { | ||
| return mc.player == null ? null : get(mc.player); | ||
| } | ||
|
|
||
| public static ClientPlayerWrapper get(AbstractClientPlayerEntity player) { | ||
| return get((ClientPlayerEntity)player); | ||
| } | ||
|
|
||
| public static ClientPlayerWrapper get(LivingEntity entity) { | ||
| return get((ClientPlayerEntity)entity); | ||
| } | ||
|
|
||
| // All static getOrDefault methods grouped together | ||
| @Nullable | ||
| public static ClientPlayerWrapper getOrDefault(LivingEntity entity) { | ||
| return entity instanceof ClientPlayerEntity ? get(entity) : null; | ||
| } | ||
|
|
||
| @Nullable | ||
| public static ClientPlayerWrapper getOrDefault(LivingEntityWrapper entityWrapper) { | ||
| return getOrDefault(entityWrapper.getInstance()); | ||
| } | ||
|
|
||
| // All static is methods grouped together | ||
| public static boolean is(EntityWrapper playerWrapper) { | ||
| return playerWrapper.getInstance() instanceof ClientPlayerEntity; | ||
| } | ||
|
|
||
| // All get methods grouped together | ||
| @Override | ||
| public ClientPlayerEntity getInstance() { | ||
| return player; | ||
| } | ||
|
|
||
| public double getLeftImpulse() { | ||
| return player.input.leftImpulse; | ||
| } | ||
|
|
||
| public double getForwardImpulse() { | ||
| return player.input.forwardImpulse; | ||
| } | ||
|
|
||
| public long getGameTime() { | ||
| return player.level.getGameTime(); | ||
| } | ||
|
|
||
| // All is/has methods grouped together | ||
| public boolean isSprinting() { | ||
| return player.isSprinting(); | ||
| } | ||
|
|
||
| public boolean isAnyMoveKeyDown() { | ||
| return player.input.up | ||
| || player.input.down | ||
| || player.input.left | ||
| || player.input.right; | ||
| } | ||
|
|
||
| public boolean hasForwardImpulse() { | ||
| return player.input.hasForwardImpulse(); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/alrex/parcool/api/compatibility/ContainersWrapper.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,15 @@ | ||
| package com.alrex.parcool.api.compatibility; | ||
|
|
||
| import net.minecraft.inventory.InventoryHelper; | ||
| import net.minecraft.item.ItemStack; | ||
| import net.minecraft.world.World; | ||
|
|
||
| public class ContainersWrapper { | ||
| public static void dropItemStack(LevelWrapper level, double x, double y, double z, ItemStack stack) { | ||
| InventoryHelper.dropItemStack(level.getInstance(), x,y, z, stack); | ||
| } | ||
|
|
||
| public static void dropItemStack(World level, double x, double y, double z, ItemStack stack) { | ||
| InventoryHelper.dropItemStack(level, x, y, z, stack); | ||
| } | ||
| } |
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.
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.
Though not limited to this class,
WeakCacheclass of this static field containsjava.util.WeakHashMaphavingAbstractClientPlayerEntityas key andAbstractClientPlayerWrapperitself as value right?But
AbstractClientPlayerWrapperhasAbstractClientPlayerEntityinstance as strong reference. If I remember correctlyWeakHashMap's keys are weak reference but the values are strong reference. So in this case the keys which are weak reference are strongly referenced by their own values. The values are strongly referenced byWeakHashMapitself. As a result I think GC cannot collect the key objects?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.
I think you're correct. To fix that I need to maintain a weak reference for the entity itself inside the wrapper, thank you for pointing that out