Skip to content

Commit

Permalink
Updated to v1.5.0
Browse files Browse the repository at this point in the history
Removed previously added feature of "last result" due to improper implementation. Did some refactoring.
Supported Minecraft version: 1.19.2
  • Loading branch information
RicoBrase committed Apr 24, 2023
1 parent e392cb3 commit d2a8ecb
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 136 deletions.
54 changes: 40 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
buildscript {
/*buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
}
}*/

plugins {
id "eclipse"
id "maven-publish"
id "net.minecraftforge.gradle" version "5.1.+"
}
apply plugin: 'net.minecraftforge.gradle'
// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.4.0'
group = 'de.ricobrase.chatcalculator' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
version = '1.5.0'
group = 'de.ricobrase.chatcalculator'
archivesBaseName = 'ChatCalculator'

java.toolchain.languageVersion = JavaLanguageVersion.of(8) // Mojang ships Java 8 to end users, so your mod should target Java 8.
java.toolchain.languageVersion = JavaLanguageVersion.of(17) // Mojang ships Java 8 to end users, so your mod should target Java 8.

println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
minecraft {
Expand All @@ -31,10 +33,10 @@ minecraft {
//
// Use non-default mappings at your own risk. they may not always work.
// Simply re-run your setup task after changing the mappings to update your workspace.
mappings channel: 'official', version: '1.16.5'
mappings channel: 'official', version: '1.19.2'
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.

// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

// Default run configurations.
// These can be tweaked, removed, or duplicated as needed.
Expand All @@ -54,8 +56,10 @@ minecraft {
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
property 'forge.logging.console.level', 'debug'

property "forge.enabledGameTestNamespaces", "ChatCalculator"

mods {
examplemod {
ChatCalculator {
source sourceSets.main
}
}
Expand All @@ -76,8 +80,26 @@ minecraft {
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
property 'forge.logging.console.level', 'debug'

property "forge.enabledGameTestNamespaces", "ChatCalculator"

mods {
ChatCalculator {
source sourceSets.main
}
}
}

gameTestServer {
workingDirectory project.file('run')

property 'forge.logging.markers', 'REGISTRIES'

property 'forge.logging.console.level', 'debug'

property 'forge.enabledGameTestNamespaces', 'ChatCalculator'

mods {
examplemod {
ChatCalculator {
source sourceSets.main
}
}
Expand All @@ -102,7 +124,7 @@ minecraft {
args '--mod', 'examplemod', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')

mods {
examplemod {
ChatCalculator {
source sourceSets.main
}
}
Expand All @@ -117,7 +139,7 @@ dependencies {
// Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed
// that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied.
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
minecraft 'net.minecraftforge:forge:1.16.5-36.2.34'
minecraft 'net.minecraftforge:forge:1.19.2-43.2.0'

// You may put jars on which you depend on in ./libs or you may define them like so..
// compile "some.group:artifact:version:classifier"
Expand Down Expand Up @@ -182,4 +204,8 @@ repositories {
mavenCentral()
}

tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
}

apply from: "https://moddingtutorials.org/applesilicon.gradle"
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.daemon=false
mc_version=1.16.5
mc_version=1.19.2
6 changes: 6 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = "https://maven.minecraftforge.net/" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ChatCalculatorMod {
public static final String MODID = "chatcalculator";
public static final Logger LOGGER = LogManager.getLogger();

private void setup(final FMLCommonSetupEvent event) {
@SuppressWarnings("unused")
private void setup(final FMLCommonSetupEvent ignoredEvent) {
LOGGER.info(ChatCalculatorMod.NAME + " is initializing...");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public enum TranslationMessages {
GLOBAL_CALC("chat.chatcalculator.globalcalcmessage"),
INVALID_CHARACTERS("chat.chatcalculator.invalidcharacters"),
NO_PREVIOUS_RESULT("chat.chatcalculator.nopreviousresult");
INVALID_CHARACTERS("chat.chatcalculator.invalidcharacters");

private final String translationKey;
TranslationMessages(String translationKey) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/dev/ricobrase/chatcalculator/Util.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package dev.ricobrase.chatcalculator;

public class Util {
public final class Util {
private Util() {

}

public static String convertDoubleToString(double input) {
if (input == Math.floor(input) && !Double.isInfinite(input) && input <= Integer.MAX_VALUE && input >= Integer.MIN_VALUE) {
input = Math.floor(input);
return String.format("%d", (int)input);
return String.format("%d", (int)Math.floor(input));
}
return String.format("%f", input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,71 @@
import dev.ricobrase.chatcalculator.TranslationMessages;
import dev.ricobrase.chatcalculator.Util;
import dev.ricobrase.chatcalculator.termsolver.TermSolver;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Optional;
import java.util.UUID;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class ClientChatEventListener {

private static String lastResult = null;

@SubscribeEvent
public static void onClientChatEvent(final ClientChatEvent event) {

String chatMessage = event.getMessage();

// Double equals (==) escaping
if(chatMessage.matches("={2}[^=]*")) {
event.setMessage(chatMessage.substring(1));
event.originalMessage = chatMessage.substring(1);
return;
}

if(!chatMessage.matches("=[^=]*")) {
return;
}

processChatCalculationMessage(event);
}

private static void processChatCalculationMessage(final ClientChatEvent clientChatEvent) {
if(Minecraft.getInstance().player == null) {
return;
}

if(chatMessage.matches("=[^=]*")) {
String termToCalculate = chatMessage.substring(1);

if(termToCalculate.contains("$0")) {
if (lastResult == null) {
printTranslatedErrorMessage(TranslationMessages.NO_PREVIOUS_RESULT);
event.setCanceled(true);
return;
}

termToCalculate = termToCalculate.replace("$0", lastResult.replace(',', '.'));
}

Optional<String> postfix = TermSolver.transformInfixToPostfix(termToCalculate);
if(postfix.isPresent()) {
try {
double result = TermSolver.solvePostfix(postfix.get());
lastResult = Util.convertDoubleToString(result);

Minecraft.getInstance().player.sendMessage(new StringTextComponent(termToCalculate), UUID.randomUUID());

String resultString = String.format("= %s", Util.convertDoubleToString(result));
Minecraft.getInstance().player.sendMessage(new StringTextComponent(resultString), UUID.randomUUID());

}catch (NumberFormatException ex) {
printTranslatedErrorMessage(TranslationMessages.INVALID_CHARACTERS);
}
}else{
printTranslatedErrorMessage(TranslationMessages.INVALID_CHARACTERS);
}
event.setCanceled(true);
String termToCalculate = clientChatEvent.getMessage().substring(1);

Optional<String> postfix = TermSolver.transformInfixToPostfix(termToCalculate);
if(postfix.isEmpty()) {
printTranslatedErrorMessage(TranslationMessages.INVALID_CHARACTERS);
clientChatEvent.setCanceled(true);
return;
}

try {
double result = TermSolver.solvePostfix(postfix.get());

Minecraft.getInstance().player.sendSystemMessage(Component.literal(termToCalculate));

String resultString = String.format("= %s", Util.convertDoubleToString(result));
Minecraft.getInstance().player.sendSystemMessage(Component.literal(resultString));

}catch (NumberFormatException ex) {
printTranslatedErrorMessage(TranslationMessages.INVALID_CHARACTERS);
}

clientChatEvent.setCanceled(true);
}

private static void printTranslatedErrorMessage(TranslationMessages message) {
if(Minecraft.getInstance().player == null) return;

Style redColor = Style.EMPTY.withColor(TextFormatting.RED);
Minecraft.getInstance().player.sendMessage(new TranslationTextComponent(message.getTranslationKey()).setStyle(redColor), UUID.randomUUID());
Style redColor = Style.EMPTY.withColor(ChatFormatting.RED);
Minecraft.getInstance().player.sendSystemMessage(Component.translatable(message.getTranslationKey()).setStyle(redColor));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import dev.ricobrase.chatcalculator.TranslationMessages;
import dev.ricobrase.chatcalculator.Util;
import dev.ricobrase.chatcalculator.termsolver.TermSolver;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.*;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class ServerChatEventListener {
Expand All @@ -24,18 +26,18 @@ public static void onServerChatEvent(ServerChatEvent event) {
return;
}

String chatMessage = event.getMessage();
ServerPlayerEntity player = event.getPlayer();
String chatMessage = event.getMessage().getString();
ServerPlayer player = event.getPlayer();

if(chatMessage.matches("@=[^=]*")) {
Optional<String> postfix = TermSolver.transformInfixToPostfix(chatMessage.substring(2));
if(postfix.isPresent()) {
try {
double result = TermSolver.solvePostfix(postfix.get());
server.getPlayerList().broadcastMessage(new TranslationTextComponent(TranslationMessages.GLOBAL_CALC.getTranslationKey(), player.getDisplayName(), chatMessage.substring(2)), ChatType.CHAT, UUID.randomUUID());
server.getPlayerList().broadcastSystemMessage(Component.translatable(TranslationMessages.GLOBAL_CALC.getTranslationKey(), player.getDisplayName(), chatMessage.substring(2)), false);

String resultString = String.format("= %s", Util.convertDoubleToString(result));
server.getPlayerList().broadcastMessage(new StringTextComponent(resultString), ChatType.CHAT, UUID.randomUUID());
server.getPlayerList().broadcastSystemMessage(Component.literal(resultString), false);

}catch (NumberFormatException ex) {
printTranslatedErrorMessage(player, TranslationMessages.INVALID_CHARACTERS);
Expand All @@ -47,9 +49,10 @@ public static void onServerChatEvent(ServerChatEvent event) {
}
}

private static void printTranslatedErrorMessage(ServerPlayerEntity player, TranslationMessages message) {
Style redColor = Style.EMPTY.withColor(TextFormatting.RED);
player.sendMessage(new TranslationTextComponent(message.getTranslationKey()).setStyle(redColor), UUID.randomUUID());
@SuppressWarnings("SameParameterValue")
private static void printTranslatedErrorMessage(@NotNull ServerPlayer player, @NotNull TranslationMessages message) {
Style redColor = Style.EMPTY.withColor(ChatFormatting.RED);
player.sendSystemMessage(Component.translatable(message.getTranslationKey(), redColor));
}

}
Loading

0 comments on commit d2a8ecb

Please sign in to comment.