Skip to content

Commit

Permalink
Revert "First Move"
Browse files Browse the repository at this point in the history
This reverts commit f6bf05c.
  • Loading branch information
MUYUTwilighter committed Jun 25, 2023
1 parent f6bf05c commit b62fdf7
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 48 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Beehave

A server-side only mod that provide information about beehives and bees.

For Fabric version, [Fabric API](https://modrinth.com/mod/fabric-api) is required.

For forge version, view [other branches](https://github.com/Nova-Committee/beehave/branches) maintained by [MikhailTapio](https://github.com/MikhailTapio)

Recommended to use along with [MoreBeeInfo](https://modrinth.com/mod/morebeeinfo).

Thank [strrraicato_](https://space.bilibili.com/314823790) for the name "Beehave" provided by them.

## Main feature

- Right-click on a beehive block or bee nest block,
bees' information will be printed into the chat HUD.
Included: bees' count, is a bee adult or baby,
how many ticks has a bee stayed in the hive,
and how many ticks are needed to be occupied before leaving the hive.
![](https://cdn-raw.modrinth.com/data/e0YVwkW5/images/49bcad6da7d3c05e070e416c277e14982ad54a61.png)
- Right-click on a bee entity, bee's information will be printed into chat HUD.
If the bee gets a hive to enter, the position of the hive will be displayed;
if not, the bee will be marked "Homeless"
![](https://cdn-raw.modrinth.com/data/e0YVwkW5/images/2956e8851b0db469131875c78ae62080abcd45cd.png)

All the operations above will be automatically filtered
if the player is holding items that got other functions.

## Plans
- Add a blacklist & whitelist that limits the items held by players to display the information
- More language support
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ processResources {
}
}

def targetJavaVersion = 16
def targetJavaVersion = 17
tasks.withType(JavaCompile).configureEach {
// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
Expand Down
13 changes: 5 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
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.1
maven_group=cool.muyucloud
archives_base_name=beehave-fabric-1.17.1
archives_base_name=beehave-fabric-1.20.1
# Dependencies
# check this on https://modmuss50.me/fabric.html

minecraft_version=1.17.1
yarn_mappings=1.17.1+build.65
loader_version=0.14.21

#Fabric api
fabric_version=0.46.1+1.17
fabric_version=0.84.0+1.20.1
4 changes: 2 additions & 2 deletions src/main/java/cool/muyucloud/beehave/Beehave.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cool.muyucloud.beehave.config.Config;
import cool.muyucloud.beehave.util.TranslatorManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.item.Item;
Expand All @@ -30,7 +30,7 @@ public void onInitialize() {
VALID_ITEMS.add(Items.AIR);
LOGGER.info("Registering command");
Event<CommandRegistrationCallback> event = CommandRegistrationCallback.EVENT;
event.register((dispatcher, environment) -> BeehaveCommand.register(dispatcher));
event.register((dispatcher, registryAccess, environment) -> BeehaveCommand.register(dispatcher));
LOGGER.info("Registering lifecycle events");
ServerLifecycleEvents.SERVER_STARTING.register(server -> CONFIG.load());
ServerLifecycleEvents.SERVER_STOPPING.register(server -> CONFIG.save());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ private static int showAll(ServerCommandSource source) {
.append(CONFIG.getAsString(key))
.append(" ");
}
source.sendFeedback(text, false);
source.sendFeedback(() -> text, false);
return 1;
}

private static MutableText getStyledKey(String key) {
MutableText info = TRANSLATOR.translate(String.format("message.command.beehave.info.%s", key));
String suggest = String.format("/beehave %s ", key);
return new LiteralText(key).setStyle(Style.EMPTY.withBold(true).withColor(Formatting.GREEN)
MutableText info = TRANSLATOR.translate("message.command.beehave.info.%s".formatted(key));
String suggest = "/beehave %s ".formatted(key);
return Text.literal(key).setStyle(Style.EMPTY.withBold(true).withColor(Formatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggest))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, info)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void register(LiteralArgumentBuilder<ServerCommandSource> parent)
private static int load(ServerCommandSource source) {
if (CONFIG.load()) {
MutableText text = TRANSLATOR.translate("message.command.beehave.load.success");
source.sendFeedback(text, false);
source.sendFeedback(() -> text, false);
return 1;
} else {
MutableText text = TRANSLATOR.translate("message.command.beehave.load.fail");
Expand All @@ -34,7 +34,7 @@ private static int load(ServerCommandSource source) {
private static int save(ServerCommandSource source) {
if (CONFIG.save()) {
MutableText text = TRANSLATOR.translate("message.command.beehave.save.success");
source.sendFeedback(text, false);
source.sendFeedback(() -> text, false);
return 1;
} else {
MutableText text = TRANSLATOR.translate("message.command.beehave.save.fail");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static JsonPrimitive readArgument(Class<? extends Serializable> type, Co
private static int set(ServerCommandSource source, String key, JsonPrimitive value) {
if (CONFIG.set(key, value)) {
MutableText text = TRANSLATOR.translate("message.command.beehave.property.set.success", key, value.getAsString());
source.sendFeedback(text, false);
source.sendFeedback(() -> text, false);
return 1;
} else {
MutableText text = TRANSLATOR.translate("message.command.beehave.property.set.fail", key, value.getAsString());
Expand All @@ -117,7 +117,7 @@ private static int get(ServerCommandSource source, String key) {
String value = CONFIG.getAsString(key);
if (value != null) {
MutableText text = TRANSLATOR.translate("message.command.beehave.property.get.success", key, value);
source.sendFeedback(text, false);
source.sendFeedback(() -> text, false);
return 1;
} else {
MutableText text = TRANSLATOR.translate("message.command.beehave.property.get.failure", key);
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/cool/muyucloud/beehave/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.google.gson.*;
import cool.muyucloud.beehave.Beehave;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger;

import java.io.InputStream;
Expand All @@ -15,7 +14,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Config {
Expand Down Expand Up @@ -72,8 +70,7 @@ private void dump() {

public Set<String> getProperties() {
HashSet<String> out = new HashSet<>();
for (Map.Entry<String, JsonElement> entry : this.content.entrySet()) {
String key = entry.getKey();
for (String key : this.content.keySet()) {
if (this.content.get(key).isJsonArray()) {
continue;
}
Expand All @@ -84,7 +81,7 @@ public Set<String> getProperties() {

public Class<? extends Serializable> getType(String key) {
if (!this.content.has(key)) {
throw new NullPointerException(String.format("Tried to access property %s but it does not exists!", key));
throw new NullPointerException("Tried to access property %s but it does not exists!".formatted(key));
}
JsonPrimitive primitive = this.content.getAsJsonPrimitive(key);
if (primitive.isBoolean()) {
Expand Down Expand Up @@ -142,7 +139,7 @@ private static boolean tryDeployFile() {
try {
Files.createFile(CONFIG_PATH);
} catch (Exception e) {
LOGGER.error(String.format("Failed to generate config file at %s.", CONFIG_PATH));
LOGGER.error("Failed to generate config file at %s.".formatted(CONFIG_PATH));
e.printStackTrace();
return false;
}
Expand All @@ -153,7 +150,7 @@ private static boolean tryDeployFile() {
private void readFile() {
try (InputStream stream = Files.newInputStream(CONFIG_PATH)) {
JsonObject object = (new Gson()).fromJson(
IOUtils.toString(stream, StandardCharsets.UTF_8),
new String(stream.readAllBytes(), StandardCharsets.UTF_8),
JsonObject.class
);
this.readConfig(object);
Expand All @@ -171,8 +168,7 @@ private void readConfig(JsonObject object) {
}

private void readProperties(JsonObject object) {
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
String key = entry.getKey();
for (String key : object.keySet()) {
if (this.content.has(key)) {
JsonPrimitive dst = this.content.getAsJsonPrimitive(key);
JsonPrimitive src = object.getAsJsonPrimitive(key);
Expand All @@ -195,7 +191,7 @@ private void readItemArray(String key, JsonObject object) {
JsonArray src = object.getAsJsonArray(key);
for (JsonElement element : src) {
Identifier id = new Identifier(element.getAsString());
if (Registry.ITEM.containsId(id)) {
if (Registries.ITEM.containsId(id)) {
dst.add(id.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import net.minecraft.entity.passive.BeeEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
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;
Expand All @@ -35,19 +35,18 @@ public void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<A
return;
}
boolean holdBreedingItem = ((AnimalEntity) (Object) this).isBreedingItem(player.getStackInHand(hand));
if (this.world.isClient || hand.equals(Hand.OFF_HAND) || holdBreedingItem) {
if (this.getWorld().isClient || hand.equals(Hand.OFF_HAND) || holdBreedingItem) {
return;
}
if (!(((AnimalEntity) (Object) this) instanceof BeeEntity)) {
if (!(((AnimalEntity) (Object) this) instanceof BeeEntity entity)) {
return;
}
BeeEntity entity = ((BeeEntity) (Object) this);
MutableText beeInfo = getBeeInfo(entity);
player.sendMessage(beeInfo, false);
}

private static MutableText getBeeInfo(BeeEntity entity) {
MutableText text = new LiteralText("").append(entity.getName()).append(": ");
MutableText text = Text.literal("").append(entity.getName()).append(": ");
if (entity.getHivePos() != null && hiveAvailable(entity)) {
BlockPos pos = entity.getHivePos();
text.append(TRANSLATOR.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -50,17 +49,17 @@ private void onUse(BlockState state, World world, BlockPos pos, PlayerEntity pla
}
BeehiveBlockEntity be = (BeehiveBlockEntity) world.getBlockEntity(pos);
if (be == null || be.hasNoBees()) {
player.sendMessage(genTextEmpty(pos), false);
player.sendMessage(genTextEmpty(pos));
return;
}
player.sendMessage(getBeesInfo(pos, be), false);
player.sendMessage(getBeesInfo(pos, be));
}

@NotNull
private static MutableText getBeesInfo(BlockPos pos, BeehiveBlockEntity be) {
NbtList bees = be.getBees();
MutableText text = TRANSLATOR.translate("message.chat.beehive.title",
be.getBeeCount(), pos.getX(), pos.getY(), pos.getZ());
be.getBeeCount(), pos.getX(), pos.getY(), pos.getZ());
for (NbtElement element : bees) {
text.append("\n");
NbtCompound compound = (NbtCompound) element;
Expand All @@ -81,7 +80,7 @@ private static MutableText getBeeInfo(NbtCompound compound) {
}

private static MutableText readName(NbtCompound entityData) {
MutableText name = new LiteralText("").append(EntityType.BEE.getName());
MutableText name = Text.literal("").append(EntityType.BEE.getName());
if (entityData.contains("CustomName")) {
name = Text.Serializer.fromJson(entityData.getString("CustomName"));
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/cool/muyucloud/beehave/util/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import cool.muyucloud.beehave.Beehave;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import org.apache.commons.io.IOUtils;
Expand All @@ -29,9 +28,9 @@ public String getLangName() {

public MutableText translate(String key, Object... args) {
if (mappings.containsKey(key)) {
return new LiteralText(String.format(mappings.get(key), args));
return Text.literal((mappings.get(key).formatted(args)));
} else {
return new LiteralText(key);
return Text.literal(key);
}
}

Expand All @@ -45,11 +44,11 @@ private HashMap<String, String> readLangFile() {
json = IOUtils.toString(
Objects.requireNonNull(
Translator.class.getClassLoader().getResource(
String.format("assets/beehave/lang/%s.json",
this.langName))),
"assets/beehave/lang/%s.json"
.formatted(this.langName))),
StandardCharsets.UTF_8);
} catch (Exception e) {
Beehave.LOGGER.error(String.format("target language file %s not present", this.langName));
Beehave.LOGGER.error("target language file %s not present".formatted(this.langName));
this.bad = true;
}
return new Gson().fromJson(json, new TypeToken<HashMap<String, String>>() {}.getType());
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/beehave.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "cool.muyucloud.beehave.mixin",
"compatibilityLevel": "JAVA_16",
"compatibilityLevel": "JAVA_17",
"mixins": [
"AnimalEntityMixin",
"BeeEntityAccessor",
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.17"
"minecraft": "~1.20"
}
}

0 comments on commit b62fdf7

Please sign in to comment.