Skip to content

Commit

Permalink
feat:加入电量变化显示
Browse files Browse the repository at this point in the history
  • Loading branch information
hirrao committed Nov 18, 2024
1 parent fdfffef commit b62c120
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 83 deletions.
118 changes: 50 additions & 68 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,126 +1,108 @@
# Gradle Properties
org.gradle.jvmargs = -Xmx3G

org.gradle.jvmargs=-Xmx3G
# Source Options
# Use Modern Java(9+) Syntax (Courtesy of Jabel)
use_modern_java_syntax = false

use_modern_java_syntax=false
# Compilation Options
generate_sources_jar = true
generate_javadocs_jar = false

generate_sources_jar=true
generate_javadocs_jar=false
# Testing
enable_junit_testing = true
show_testing_output = false

enable_junit_testing=true
show_testing_output=false
# Mod Information
# HIGHLY RECOMMEND complying with SemVer for mod_version: https://semver.org/
mod_version = 0.0.1-beta
root_package = com.hirrao.WirelessEnergy
mod_id = wirelessenergy
mod_name = WirelessEnergy

mod_version=0.0.2-beta
root_package=com.hirrao.WirelessEnergy
mod_id=wirelessenergy
mod_name=WirelessEnergy
# Mod Metadata (Optional)
mod_description =
mod_url =
mod_update_json =
mod_description=
mod_url=
mod_update_json=
# Delimit authors with commas
mod_authors = hirrao
mod_credits =
mod_logo_path =

mod_authors=hirrao
mod_credits=
mod_logo_path=
# Mapping Properties
mapping_channel = stable
mapping_version = 39
use_dependency_at_files = true

mapping_channel=stable
mapping_version=39
use_dependency_at_files=true
# Run Configurations
# If multiple arguments/tweak classes are stated, use spaces as the delimiter
minecraft_username = Developer
extra_jvm_args =
extra_tweak_classes =

minecraft_username=Developer
extra_jvm_args=
extra_tweak_classes=
# Maven Publishing (Provide secret: MAVEN_USER, MAVEN_PASS)
publish_to_maven = false
publish_to_maven=false
# Good for debugging artifacts before uploading to remote maven
# GitHub actions won't run if this is true, test this by running the task `publishToMavenLocal`
publish_to_local_maven = false
maven_name = ${mod_name}
maven_url =

publish_to_local_maven=false
maven_name=${mod_name}
maven_url=
# Publishing
# release_type can only be: release, beta or alpha (applies to CurseForge / Modrinth)
release_type = release
publish_with_changelog = ${{ it.file('CHANGELOG.md').exists() }}

release_type=release
publish_with_changelog=${{ it.file('CHANGELOG.md').exists() }}
# Publishing to CurseForge (Provide secret: CURSEFORGE_TOKEN)
# To configure dependencies, head to publishing.gradle's curseforge block
publish_to_curseforge = false
publish_to_curseforge=false
# CurseForge project ID must be the numerical ID and not the slug
curseforge_project_id =
curseforge_debug = false

curseforge_project_id=
curseforge_debug=false
# Publishing to Modrinth (Provide secret: MODRINTH_TOKEN), the token must have the `CREATE_VERSION` and `PROJECT_WRITE` permissions
# To configure dependencies, head to publishing.gradle's modrinth block
publish_to_modrinth = false
modrinth_project_id =
publish_to_modrinth=false
modrinth_project_id=
# Allows gradle to publish updated READMEs to the project body (via the modrinthSyncBody task)
modrinth_sync_readme = false
modrinth_debug = false

modrinth_sync_readme=false
modrinth_debug=false
# If any properties changes below this line, refresh gradle again to ensure everything is working correctly.

# Modify Minecraft Sources
# RetroFuturaGradle allows Minecraft sources to be edited, and have the changes reflected upon running it
# Good for previews when coremodding, or generally seeing how behaviours can change with certain code applied/unapplied
# Turning this on allows Minecraft sources to persist and not regenerate
change_minecraft_sources = false

change_minecraft_sources=false
# Tags
# A RetroFuturaGradle concept akin to Ant ReplaceTokens
# A class is generated at build-time for compilation, to describe properties that have values that could change at build time such as versioning
# Class name is configurable with the `tag_class_name` property
# Tag properties can be stated in the `tags.properties` file, references are allowed
use_tags = true
tag_class_name = ${root_package}.${mod_id}.Tags

use_tags=true
tag_class_name=${root_package}.${mod_id}.Tags
# Access Transformers
# A way to change visibility of Minecraft's classes, methods and fields
# An example access transformer file is given in the path: `src/main/resources/example_at.cfg`
# AT files should be in the root of src/main/resources with the filename formatted as: `mod_id_at.cfg`
# Use the property `access_transformer_locations` to state custom AT files if you aren't using the default `mod_id_at.cfg` location
# If multiple locations are stated, use spaces as the delimiter
use_access_transformer = false
access_transformer_locations = ${mod_id}_at.cfg

use_access_transformer=false
access_transformer_locations=${mod_id}_at.cfg
# Mixins
# Powerful tool to do runtime description changes of classes
# Wiki: https://github.com/SpongePowered/Mixin/wiki + https://github.com/CleanroomMC/MixinBooter/ + https://cleanroommc.com/wiki/forge-mod-development/mixin/preface
# Only use mixins once you understand the underlying structure
use_mixins = false
mixin_booter_version = 9.1
use_mixins=false
mixin_booter_version=9.1
# A configuration defines a mixin set, and you may have as many mixin sets as you require for your application.
# Each config can only have one and only one package root.
# Generate missing configs, obtain from mixin_configs and generate file base on name convention: "mixins.config_name.json"
# You should change package root once they are generated
generate_mixins_json = true
generate_mixins_json=true
# Delimit configs with spaces. Should only put configs name instead of full file name
mixin_configs = ${mod_id}
mixin_configs=${mod_id}
# A refmap is a json that denotes mapping conversions, this json is generated automatically, with the name `mixins.mod_id.refmap.json`
# Use the property `mixin_refmap` if you want it to use a different name, only one name is accepted
mixin_refmap = mixins.${mod_id}.refmap.json

mixin_refmap=mixins.${mod_id}.refmap.json
# Coremods
# The most powerful way to change java classes at runtime, it is however very primitive with little documentation.
# Only make a coremod if you are absolutely sure of what you are doing
# Change the property `coremod_includes_mod` to false if your coremod doesn't have a @Mod annotation
# You MUST state a class name for `coremod_plugin_class_name` if you are making a coremod, the class should implement `IFMLLoadingPlugin`
is_coremod = false
coremod_includes_mod = true
coremod_plugin_class_name =

is_coremod=false
coremod_includes_mod=true
coremod_plugin_class_name=
# AssetMover
# Convenient way to allow downloading of assets from official vanilla Minecraft servers, CurseForge, or any direct links
# Documentation: https://github.com/CleanroomMC/AssetMover
use_asset_mover = false
asset_mover_version = 2.5
use_asset_mover=false
asset_mover_version=2.5
24 changes: 15 additions & 9 deletions src/main/java/com/hirrao/WirelessEnergy/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.UUID;

import static com.hirrao.WirelessEnergy.EventTick.totalEnergyLast;
import static com.hirrao.WirelessEnergy.EventTick.totalEnergyNow;

public class Command extends CommandBase {

@Override
Expand Down Expand Up @@ -43,14 +44,19 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
return;
}
try {
Class<?> clazz = Class.forName("magicbook.gtlitecore.api.misc.GlobalVariableStorage");
Field field = clazz.getDeclaredField("WirelessEnergy");
@SuppressWarnings("unchecked") HashMap<UUID, BigInteger> wirelessEnergy = (HashMap<UUID, BigInteger>) field.get(null);
BigInteger energy = wirelessEnergy.getOrDefault(uuid, BigInteger.ZERO);
sender.sendMessage(new TextComponentString("Energy: " + energy));

BigInteger energyNow = totalEnergyNow.getOrDefault(uuid, BigInteger.ZERO);
BigInteger energyLast = totalEnergyLast.getOrDefault(uuid, BigInteger.ZERO);
BigInteger energyDiff = energyNow.subtract(energyLast);
Log.LOGGER.info("energyNow: " + energyNow + ", energyLast: " + energyLast + ", energyDiff: " + energyDiff);
if (energyDiff.compareTo(BigInteger.ZERO) >= 0) {
sender.sendMessage(new TextComponentString("当前能量" + energyNow + ",增加了" + energyDiff));
} else if (energyDiff.compareTo(BigInteger.ZERO) < 0) {
sender.sendMessage(new TextComponentString("当前能量" + energyNow + ",减少了" + energyDiff.abs()));
BigInteger time = energyNow.divide(energyDiff.abs());
sender.sendMessage(new TextComponentString("预计" + time + "秒后能量耗尽"));
}
} catch (Exception e) {
e.printStackTrace();
Log.LOGGER.error(e);
}

}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/hirrao/WirelessEnergy/EventTick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hirrao.WirelessEnergy;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.UUID;

public class EventTick {
private static final int TICKS_PER_SECOND = 20;
public static HashMap<UUID, BigInteger> totalEnergyNow;
public static HashMap<UUID, BigInteger> totalEnergyLast;
private int tickCount = 0;

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
tickCount++;
if (tickCount >= TICKS_PER_SECOND) {
tickCount = 0;
try {
Class<?> clazz = Class.forName("magicbook.gtlitecore.api.misc.GlobalVariableStorage");
Field field = clazz.getDeclaredField("WirelessEnergy");
HashMap<UUID, BigInteger> wirelessEnergy = (HashMap<UUID, BigInteger>) field.get(null);
if (totalEnergyNow == null) {
totalEnergyNow = wirelessEnergy;
}
totalEnergyLast = new HashMap<>(totalEnergyNow);
totalEnergyNow = wirelessEnergy;
} catch (NoSuchFieldException | ClassNotFoundException e) {
Log.LOGGER.error("Failed to get WirelessEnergy from GlobalVariableStorage");
} catch (Exception e) {
Log.LOGGER.error(e);
}
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/hirrao/WirelessEnergy/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hirrao.WirelessEnergy;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log {
public static final Logger LOGGER = LogManager.getLogger(WirelessEnergy.MOD_NAME);
}
10 changes: 4 additions & 6 deletions src/main/java/com/hirrao/WirelessEnergy/WirelessEnergy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(modid = WirelessEnergy.MOD_ID, name = WirelessEnergy.MOD_NAME, version = WirelessEnergy.VERSION)
@Mod(modid = WirelessEnergy.MOD_ID, name = WirelessEnergy.MOD_NAME, version = WirelessEnergy.VERSION, dependencies = "required-after:gtlitecore@[0.0.1-alpha,)")
public class WirelessEnergy {

public static final String MOD_ID = "wirelessenergy";
public static final String MOD_NAME = "WirelessEnergy";
public static final String VERSION = "0.0.1-beta";
public static final Logger LOGGER = LogManager.getLogger(WirelessEnergy.MOD_NAME);
public static final String VERSION = "0.0.2-beta";

/**
* <a href="https://cleanroommc.com/wiki/forge-mod-development/event#overview">
Expand All @@ -22,13 +19,14 @@ public class WirelessEnergy {
*/
@Mod.EventHandler
public void onServerStarting(FMLServerStartingEvent event) {
LOGGER.info("WireLessEnergy is starting");
Log.LOGGER.info("WireLessEnergy is starting");
event.registerServerCommand(new Command());
}

@Mod.EventHandler
public void onInit(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(new Command());
MinecraftForge.EVENT_BUS.register(new EventTick());
}

}

0 comments on commit b62c120

Please sign in to comment.