Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added settings.gradle
Empty file.
4 changes: 2 additions & 2 deletions src/main/java/com/alrex/parcool/ParCool.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.alrex.parcool.common.block.Blocks;
import com.alrex.parcool.common.block.TileEntities;
import com.alrex.parcool.common.capability.capabilities.Capabilities;
import com.alrex.parcool.common.entity.EntityType;
import com.alrex.parcool.common.entity.ParcoolEntityType;
import com.alrex.parcool.common.handlers.AddAttributesHandler;
import com.alrex.parcool.common.item.Items;
import com.alrex.parcool.common.item.recipe.Recipes;
Expand Down Expand Up @@ -83,7 +83,7 @@ public ParCool() {
Blocks.register(FMLJavaModLoadingContext.get().getModEventBus());
Items.register(FMLJavaModLoadingContext.get().getModEventBus());
Recipes.register(FMLJavaModLoadingContext.get().getModEventBus());
EntityType.register(FMLJavaModLoadingContext.get().getModEventBus());
ParcoolEntityType.register(FMLJavaModLoadingContext.get().getModEventBus());
TileEntities.register(FMLJavaModLoadingContext.get().getModEventBus());

ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ParCoolConfig.Server.BUILT_CONFIG);
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/alrex/parcool/api/Stamina.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.alrex.parcool.api;

import com.alrex.parcool.api.compatibility.PlayerWrapper;
import com.alrex.parcool.common.capability.IStamina;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nullable;

public class Stamina {
@Nullable
public static Stamina get(PlayerEntity player) {
public static Stamina get(PlayerWrapper player) {
IStamina instance = IStamina.get(player);
if (instance == null) {
return null;
}
if (instance == null) return null;
return new Stamina(instance);
}

Expand Down
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<>();
Copy link
Owner

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, WeakCache class of this static field contains java.util.WeakHashMap having AbstractClientPlayerEntity as key and AbstractClientPlayerWrapper itself as value right?
But AbstractClientPlayerWrapper has AbstractClientPlayerEntity instance as strong reference. If I remember correctly WeakHashMap'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 by WeakHashMap itself. As a result I think GC cannot collect the key objects?

Copy link
Contributor Author

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


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 src/main/java/com/alrex/parcool/api/compatibility/AxisWrapper.java
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();
}
}
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;
}
}
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);
}
}
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();
}
}
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);
}
}
Loading