Skip to content

Commit

Permalink
Port to 21
Browse files Browse the repository at this point in the history
Add particles showing directions for a bee's hive
Improvements in texts color of beehive info.
  • Loading branch information
MUYUTwilighter committed Aug 10, 2024
1 parent 03650f8 commit d90b356
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.2-SNAPSHOT'
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
}

Expand Down
20 changes: 10 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.5
loader_version=0.14.21
# Mod Properties
mod_version=0.0.2
mod_version=0.0.4
maven_group=cool.muyucloud
archives_base_name=beehave-fabric-1.20.1
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.84.0+1.20.1
archives_base_name=beehave-fabric-1.21
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.21.1
yarn_mappings=1.21.1+build.1
loader_version=0.15.11

# Fabric API
fabric_version=0.102.0+1.21.1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cool.muyucloud.beehave.access;

import net.minecraft.util.math.BlockPos;

public interface BeeEntityAccess {
boolean invokeDoesHiveHaveSpace(BlockPos pos);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cool.muyucloud.beehave.access;

import net.minecraft.block.entity.BeehiveBlockEntity;

import java.util.List;

public interface BeehiveBlockEntityAccess {
List<BeehiveBlockEntity.BeeData> invokeCreateBeeData();
}
2 changes: 1 addition & 1 deletion src/main/java/cool/muyucloud/beehave/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void readItemArray(String key, JsonObject object) {
JsonArray dst = this.content.getAsJsonArray(key);
JsonArray src = object.getAsJsonArray(key);
for (JsonElement element : src) {
Identifier id = new Identifier(element.getAsString());
Identifier id = Identifier.of(element.getAsString());
if (Registries.ITEM.containsId(id)) {
dst.add(id.toString());
}
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/cool/muyucloud/beehave/mixin/AnimalEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package cool.muyucloud.beehave.mixin;

import cool.muyucloud.beehave.Beehave;
import cool.muyucloud.beehave.access.BeeEntityAccess;
import cool.muyucloud.beehave.config.Config;
import cool.muyucloud.beehave.util.TranslatorManager;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.passive.BeeEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.command.ParticleCommand;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Position;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -38,11 +46,28 @@ public void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<A
if (this.getWorld().isClient || hand.equals(Hand.OFF_HAND) || holdBreedingItem) {
return;
}
if (!(((AnimalEntity) (Object) this) instanceof BeeEntity entity)) {
if (((AnimalEntity) (Object) this) instanceof BeeEntity entity) {
MutableText beeInfo = getBeeInfo(entity);
player.sendMessage(beeInfo, false);
playParticles(entity.getHivePos(), (ServerPlayerEntity) player);
}
}

private void playParticles(@NotNull BlockPos hivePos, @NotNull ServerPlayerEntity target) {
final int density = 3;
Vec3d beePos = this.getPos();
if (!hiveAvailable((BeeEntity) (Object) this)) {
return;
}
MutableText beeInfo = getBeeInfo(entity);
player.sendMessage(beeInfo, false);
Vec3d delta = beePos.relativize(hivePos.toCenterPos());
double distance = delta.length();
Vec3d step = delta.multiply(1.0D / (density * distance), 1.0D / (density * distance), 1.0D / (density * distance));
int count = (int) (distance * density);
Vec3d pos = beePos;
for (int i = 0; i <= count; ++i) {
((ServerWorld) this.getWorld()).spawnParticles(target, ParticleTypes.HAPPY_VILLAGER, false, pos.getX(), pos.getY(), pos.getZ(), 1, 0.0D, 0.0D, 0.0D, 0.0D);
pos = pos.add(step);
}
}

private static MutableText getBeeInfo(BeeEntity entity) {
Expand All @@ -59,6 +84,6 @@ private static MutableText getBeeInfo(BeeEntity entity) {

private static boolean hiveAvailable(BeeEntity entity) {
return entity.hasHive() &&
((BeeEntityAccessor) entity).invokeDoesHiveHaveSpace(entity.getHivePos());
((BeeEntityAccess) entity).invokeDoesHiveHaveSpace(entity.getHivePos());
}
}
12 changes: 0 additions & 12 deletions src/main/java/cool/muyucloud/beehave/mixin/BeeEntityAccessor.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/java/cool/muyucloud/beehave/mixin/BeeEntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cool.muyucloud.beehave.mixin;

import cool.muyucloud.beehave.access.BeeEntityAccess;
import net.minecraft.entity.passive.BeeEntity;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(BeeEntity.class)
public abstract class BeeEntityMixin implements BeeEntityAccess {
@Shadow protected abstract boolean doesHiveHaveSpace(BlockPos pos);

@Override
public boolean invokeDoesHiveHaveSpace(BlockPos pos) {
return this.doesHiveHaveSpace(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cool.muyucloud.beehave.mixin;

import cool.muyucloud.beehave.access.BeehiveBlockEntityAccess;
import net.minecraft.block.entity.BeehiveBlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.List;

@Mixin(BeehiveBlockEntity.class)
public abstract class BeehiveBlockEntityMixin implements BeehiveBlockEntityAccess {
@Shadow protected abstract List<BeehiveBlockEntity.BeeData> createBeesData();

@Override
public List<BeehiveBlockEntity.BeeData> invokeCreateBeeData() {
List<BeehiveBlockEntity.BeeData> list = createBeesData();
return list;
}
}
47 changes: 32 additions & 15 deletions src/main/java/cool/muyucloud/beehave/mixin/BeehiveBlockMixin.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
package cool.muyucloud.beehave.mixin;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.MapDecoder;
import cool.muyucloud.beehave.Beehave;
import cool.muyucloud.beehave.access.BeehiveBlockEntityAccess;
import cool.muyucloud.beehave.config.Config;
import cool.muyucloud.beehave.util.TranslatorManager;
import net.minecraft.block.BeehiveBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BeehiveBlockEntity;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.BeeEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;
import java.util.Objects;

@Mixin(BeehiveBlock.class)
public abstract class BeehiveBlockMixin extends BlockWithEntity {
@Shadow @Final public static MapCodec<BeehiveBlock> CODEC;
private static final TranslatorManager TRANSLATOR = Beehave.TRANSLATOR;
private static final Config CONFIG = Beehave.CONFIG;

protected BeehiveBlockMixin(Settings settings) {
super(settings);
}

@Inject(method = "onUse", at = @At("HEAD"))
private void onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
@Inject(method = "onUseWithItem", at = @At("HEAD"))
private void onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ItemActionResult> cir) {
boolean enable = CONFIG.getAsBoolean("beehive");
if (world.isClient || hand.equals(Hand.OFF_HAND) || !enable) {
return;
Expand All @@ -57,32 +71,35 @@ private void onUse(BlockState state, World world, BlockPos pos, PlayerEntity pla

@NotNull
private static MutableText getBeesInfo(BlockPos pos, BeehiveBlockEntity be) {
NbtList bees = be.getBees();
List<BeehiveBlockEntity.BeeData> bees = ((BeehiveBlockEntityAccess) be).invokeCreateBeeData();
MutableText text = TRANSLATOR.translate("message.chat.beehive.title",
be.getBeeCount(), pos.getX(), pos.getY(), pos.getZ());
for (NbtElement element : bees) {
for (BeehiveBlockEntity.BeeData element : bees) {
text.append("\n");
NbtCompound compound = (NbtCompound) element;
text.append(getBeeInfo(compound));
text.append(getBeeInfo(element));
}
return text;
}

private static MutableText getBeeInfo(NbtCompound compound) {
NbtCompound entityData = compound.getCompound("EntityData");
private static MutableText getBeeInfo(BeehiveBlockEntity.BeeData beeData) {
NbtComponent entityData = beeData.entityData();
MutableText text = readName(entityData).append(": ");
String isBaby = Objects.requireNonNull(entityData).getInt("Age") < 0 ?
String isBaby = entityData.get(Codec.INT.fieldOf("Age")).getOrThrow() < 0 ?
"baby" : "adult";
int ticksInHive = compound.getInt("TicksInHive");
int minOccupationTicks = compound.getInt("MinOccupationTicks");
return text.append(TRANSLATOR.translate("message.chat.beehive.row",
int ticksInHive = beeData.ticksInHive();
int minOccupationTicks = beeData.minTicksInHive();
text.append(TRANSLATOR.translate("message.chat.beehive.row",
isBaby, ticksInHive, minOccupationTicks));
if (ticksInHive >= minOccupationTicks) {
text.withColor(Formatting.GOLD.getColorValue());
}
return text;
}

private static MutableText readName(NbtCompound entityData) {
private static MutableText readName(NbtComponent entityData) {
MutableText name = Text.literal("").append(EntityType.BEE.getName());
if (entityData.contains("CustomName")) {
name = Text.Serializer.fromJson(entityData.getString("CustomName"));
name = Text.literal(Objects.requireNonNull(entityData.get(Codec.STRING.fieldOf("CustomName")).getOrThrow()));
}
return name;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/cool/muyucloud/beehave/util/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Objects;

public class Translator {

private final String langName;
private final HashMap<String, String> mappings;
private boolean bad = false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/beehave/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"message.chat.beehive.empty": "No bee in [x=%d, y=%d, z=%d]",
"message.chat.beehive.title": "%d bee(s) in [x=%d, y=%d, z=%d]",
"message.chat.beehive.row": "is %s, hived %d tick(s), occupation %d tick(s)",
"message.chat.beehive.row": "is %s, hived %d / %d tick(s)",
"message.chat.bee.info": "Hive at [x=%d, y=%d, z=%d]",
"message.chat.bee.homeless": "Homeless",
"message.command.beehave.title": "Beehave settings overview",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/beehave/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"message.chat.beehive.empty": "位于 [x=%d, y=%d, z=%d] 的蜂巢没有蜜蜂",
"message.chat.beehive.title": "%d 只蜜蜂在位于 [x=%d, y=%d, z=%d] 的蜂巢中",
"message.chat.beehive.row": "年龄 %s, 已归巢 %d tick(s), 至少 %d tick(s) 后出巢",
"message.chat.beehive.row": "年龄 %s, 已归巢 %d %d tick(s)",
"message.chat.bee.info": "蜂巢在 [x=%d, y=%d, z=%d]",
"message.chat.bee.homeless": "没有可进入的蜂巢",
"message.command.beehave.title": "Beehave 设置概览",
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/beehave.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"required": true,
"minVersion": "0.8",
"package": "cool.muyucloud.beehave.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"mixins": [
"AnimalEntityMixin",
"BeeEntityAccessor",
"BeeEntityMixin",
"BeehiveBlockEntityMixin",
"BeehiveBlockMixin"
],
"client": [],
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"depends": {
"fabricloader": "*",
"fabric": "*",
"minecraft": "~1.20"
"minecraft": "~1.21"
}
}

0 comments on commit d90b356

Please sign in to comment.