Commit 765a259d by Jonathan Thomas

- Allow entity to respond to being handed an item automatically (limit 1 time),…

- Allow entity to respond to being handed an item automatically (limit 1 time), if they already have a character sheet. NOTE: They must need the item and accept it.
parent 458fdb2b
Pipeline #12016 passed with stage
in 19 seconds
package com.owlmaddie.goals; package com.owlmaddie.goals;
import com.owlmaddie.mixin.MixinMobEntity; import com.owlmaddie.mixin.MixinMobEntityAccessor;
import net.minecraft.entity.ai.goal.GoalSelector; import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
/** /**
* The {@code GoalUtils} class uses reflection to extend the MobEntity class * The {@code GoalUtils} class uses reflection to extend the MobEntity class
* and add a public GoalSelector method. * and add a public GoalSelector method.
...@@ -12,7 +11,7 @@ import net.minecraft.entity.mob.MobEntity; ...@@ -12,7 +11,7 @@ import net.minecraft.entity.mob.MobEntity;
public class GoalUtils { public class GoalUtils {
public static GoalSelector getGoalSelector(MobEntity mobEntity) { public static GoalSelector getGoalSelector(MobEntity mobEntity) {
MixinMobEntity mixingEntity = (MixinMobEntity)mobEntity; MixinMobEntityAccessor mixingEntity = (MixinMobEntityAccessor)mobEntity;
return mixingEntity.getGoalSelector(); return mixingEntity.getGoalSelector();
} }
} }
package com.owlmaddie.mixin; package com.owlmaddie.mixin;
import org.spongepowered.asm.mixin.Mixin; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import org.spongepowered.asm.mixin.gen.Accessor; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.ai.goal.GoalSelector; import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/** /**
* The {@code MixinMobEntity} mixin class exposes the goalSelector field from the MobEntity class. * The {@code MixinMobEntity} mixin class exposes the goalSelector field from the MobEntity class.
*/ */
@Mixin(MobEntity.class) @Mixin(MobEntity.class)
public interface MixinMobEntity { public class MixinMobEntity {
@Accessor("goalSelector") public GoalSelector getGoalSelector();
@Inject(method = "interact", at = @At(value = "RETURN"))
private void onItemGiven(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
if (!cir.getReturnValue().isAccepted() || player.getWorld().isClient()) {
// If interaction was not successful, return early
return;
}
ItemStack itemStack = player.getStackInHand(hand);
MobEntity thisEntity = (MobEntity) (Object) this;
// Check if the player successfully interacts with an item
if (!itemStack.isEmpty() && player instanceof ServerPlayerEntity) {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
String itemName = itemStack.getItem().toString();
int itemCount = itemStack.getCount();
// Prepare a message about the interaction
String giveItemMessage = "<" + serverPlayer.getName().getString() +
" hands you " + itemCount + " " + itemName + ">";
ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString());
if (!chatData.characterSheet.isEmpty() && !chatData.auto_generated) {
ServerPackets.generate_chat(chatData, serverPlayer, thisEntity, giveItemMessage, true);
}
}
}
} }
\ No newline at end of file
package com.owlmaddie.mixin;
import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.mob.MobEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
/**
* The {@code MixinMobEntityAccessor} mixin class exposes the goalSelector field from the MobEntity class.
*/
@Mixin(MobEntity.class)
public interface MixinMobEntityAccessor {
@Accessor("goalSelector") public GoalSelector getGoalSelector();
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"mixins": [ "mixins": [
"MixinMobEntity", "MixinMobEntity",
"MixinMobEntityAccessor",
"MixinLivingEntity" "MixinLivingEntity"
], ],
"injectors": { "injectors": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment