Skip to content

Commit

Permalink
look into data attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
parzivail committed Oct 24, 2024
1 parent 5165e7d commit 0f4dee2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public UseAction getUseAction(ItemStack stack)
@Override
public boolean onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks)
{
if (!world.isClient && user.getItemUseTime() > TOGGLE_AIMING_USE_TIME_TICKS && isAiming(stack))
if (!world.isClient() && user.getItemUseTime() > TOGGLE_AIMING_USE_TIME_TICKS && isAiming(stack))
setAiming(stack, false);

return super.onStoppedUsing(stack, world, user, remainingUseTicks);
Expand All @@ -148,7 +148,7 @@ public ActionResult use(World world, PlayerEntity user, Hand hand)
{
var stack = user.getStackInHand(hand);

if (!world.isClient)
if (!world.isClient())
{
setAiming(stack, !isAiming(stack));

Expand Down
12 changes: 12 additions & 0 deletions projects/pswg_core/src/main/java/dev/pswg/Galaxies.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import dev.pswg.configuration.MemoryConfigContainer;
import dev.pswg.interaction.GalaxiesEntityLeftClickManager;
import dev.pswg.interaction.GalaxiesPlayerActionManager;
import dev.pswg.interaction.LeftClickingEntityAttachment;
import dev.pswg.networking.GalaxiesPlayerActionC2SPacket;
import dev.pswg.networking.PlayerInteractItemLeftC2SPacket;
import dev.pswg.updater.GithubReleaseEntry;
import dev.pswg.updater.UpdateChecker;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -57,6 +60,15 @@ public static Identifier id(String path)
*/
public static final IConfigContainer<GalaxiesConfig> CONFIG = new MemoryConfigContainer<>(new GalaxiesConfig());

/**
* An attachment for entities that contains the data required to
* represent their left-using state
*/
public static final AttachmentType<LeftClickingEntityAttachment> LEFT_CLICKING_ATTACHMENT = AttachmentRegistry.createPersistent(
id("left_clicking_entity"),
LeftClickingEntityAttachment.CODEC
);

/**
* Create a derived logger for a subsystem within PSWG
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.pswg.interaction;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.pswg.Galaxies;
import net.minecraft.entity.LivingEntity;

public record LeftClickingEntityAttachment(boolean isUsingItemLeft, int itemLeftUseTimeLeft)
{
public static final Codec<LeftClickingEntityAttachment> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("isUsingItemLeft").forGetter(LeftClickingEntityAttachment::isUsingItemLeft),
Codec.INT.fieldOf("itemLeftUseTimeLeft").forGetter(LeftClickingEntityAttachment::itemLeftUseTimeLeft)
).apply(instance, LeftClickingEntityAttachment::new));

public static LeftClickingEntityAttachment get(LivingEntity entity)
{
return entity.getAttachedOrCreate(Galaxies.LEFT_CLICKING_ATTACHMENT, () -> new LeftClickingEntityAttachment(
false,
0
));
}

public LeftClickingEntityAttachment withIsLeftUsing(boolean isLeftUsing)
{
return new LeftClickingEntityAttachment(isLeftUsing, itemLeftUseTimeLeft);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.pswg.mixin;

import dev.pswg.Galaxies;
import dev.pswg.interaction.ILeftClickingEntity;
import dev.pswg.interaction.LeftClickingEntityAttachment;
import dev.pswg.item.ILeftClickUsable;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -13,11 +15,8 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LivingEntity.class)
public class LivingEntityMixin implements ILeftClickingEntity
public abstract class LivingEntityMixin implements ILeftClickingEntity
{
@Unique
private boolean isUsingItemLeft;

@Unique
private int itemLeftUseTimeLeft;

Expand All @@ -27,17 +26,20 @@ public class LivingEntityMixin implements ILeftClickingEntity
@Override
public boolean pswg$isLeftUsingItem()
{
return isUsingItemLeft;
var self = (LivingEntity)(Object)this;

var attachment = LeftClickingEntityAttachment.get(self);
return attachment.isUsingItemLeft();
}

@Override
public void pswg$setLeftUsingItem(boolean isLeftUsing)
{
// TODO: find all setters
isUsingItemLeft = isLeftUsing;

var self = (LivingEntity)(Object)this;

var attachment = LeftClickingEntityAttachment.get(self);
self.setAttached(Galaxies.LEFT_CLICKING_ATTACHMENT, attachment.withIsLeftUsing(isLeftUsing));

if (this.pswg$isLeftUsingItem() && this.pswg$getLeftActiveItemStack().isEmpty())
{
var activeStack = self.getStackInHand(self.getActiveHand());
Expand Down Expand Up @@ -136,7 +138,7 @@ else if (!this.pswg$isLeftUsingItem() && !this.pswg$getLeftActiveItemStack().isE
if (!(stack.getItem() instanceof ILeftClickUsable leftClickingItem))
throw new RuntimeException("Attempted to tick usage of non-left-clicking item");

if (this.pswg$getItemLeftUseTimeLeft() == 0 && !self.getWorld().isClient && !leftClickingItem.isUsedOnLeftRelease(stack))
if (this.pswg$getItemLeftUseTimeLeft() == 0 && !self.getWorld().isClient() && !leftClickingItem.isUsedOnLeftRelease(stack))
{
this.pswg$consumeLeftItem();
}
Expand All @@ -147,7 +149,7 @@ else if (!this.pswg$isLeftUsingItem() && !this.pswg$getLeftActiveItemStack().isE
{
var self = (LivingEntity)(Object)this;

if (!self.getWorld().isClient || this.pswg$isLeftUsingItem())
if (!self.getWorld().isClient() || this.pswg$isLeftUsingItem())
{
Hand hand = self.getActiveHand();
if (!this.pswg$getLeftActiveItemStack().equals(self.getStackInHand(hand)))
Expand Down Expand Up @@ -189,7 +191,7 @@ else if (!this.pswg$isLeftUsingItem() && !this.pswg$getLeftActiveItemStack().isE

this.pswg$setItemLeftUseTimeLeft(leftClickingItem.getMaxUseLeftTime(itemStack, self));

if (!self.getWorld().isClient)
if (!self.getWorld().isClient())
{
this.pswg$setLeftUsingItem(true);

Expand All @@ -206,7 +208,7 @@ else if (!this.pswg$isLeftUsingItem() && !this.pswg$getLeftActiveItemStack().isE
{
var self = (LivingEntity)(Object)this;

if (!self.getWorld().isClient)
if (!self.getWorld().isClient())
{
boolean wasUsingItem = this.pswg$isLeftUsingItem();
this.pswg$setLeftUsingItem(false);
Expand Down

0 comments on commit 0f4dee2

Please sign in to comment.