initial commit, command hooks, messenger and test command

This commit is contained in:
gnembon committed 2019-05-10 00:05:25 -03:00
1 parent a649a8fdb1
commit bddad16344
19 files changed
+1064 -2

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
package carpet;
//import carpet.commands.CameraModeCommand;
//import carpet.commands.CarpetCommand;
//import carpet.commands.CounterCommand;
//import carpet.commands.DistanceCommand;
//import carpet.commands.DrawCommand;
//import carpet.commands.ScriptCommand;
//import carpet.commands.InfoCommand;
//import carpet.commands.LogCommand;
//import carpet.commands.PerimeterInfoCommand;
//import carpet.commands.PlayerCommand;
//import carpet.commands.SpawnCommand;
import carpet.commands.TestCommand;
//import carpet.commands.TickCommand;
//import carpet.logging.LoggerRegistry;
//import carpet.script.ExpressionInspector;
//import carpet.utils.HUDController;
import java.util.Random;
//import carpet.helpers.TickSpeed;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
public class CarpetServer // static for now - easier to handle all around the code, its one anyways
{
public static final Random rand = new Random((int)((2>>16)*Math.random()));
public static MinecraftServer minecraft_server;
public static void init(MinecraftServer server) //aka constructor of this static singleton class
{
CarpetServer.minecraft_server = server;
}
public static void onServerLoaded(MinecraftServer server)
{
//CarpetSettings.apply_settings_from_conf(server);
//ExpressionInspector.CarpetExpression_resetExpressionEngine();
}
// Separate from onServerLoaded, because a server can be loaded multiple times in singleplayer
public static void onGameStarted() {
//LoggerRegistry.initLoggers();
}
public static void tick(MinecraftServer server)
{
//TickSpeed.tick(server);
//HUDController.update_hud(server);
}
public static void registerCarpetCommands(CommandDispatcher<ServerCommandSource> dispatcher)
{
//CarpetCommand.register(dispatcher);
//TickCommand.register(dispatcher);
//CounterCommand.register(dispatcher);
//LogCommand.register(dispatcher);
//SpawnCommand.register(dispatcher);
//PlayerCommand.register(dispatcher);
//CameraModeCommand.register(dispatcher);
//InfoCommand.register(dispatcher);
//DistanceCommand.register(dispatcher);
//PerimeterInfoCommand.register(dispatcher);
//DrawCommand.register(dispatcher);
//ScriptCommand.register(dispatcher);
TestCommand.register(dispatcher);
}
}
@@ -0,0 +1,29 @@
package carpet.commands;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public class TestCommand
{
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
dispatcher.register(literal("test").
then(argument("first",word()).
executes( (c)-> test(c, getString(c, "first")+" 1"))).
then(argument("second", word()).
executes( (c)-> test(c, getString(c, "second")+" 2"))));
}
private static int test(CommandContext<ServerCommandSource> c, String term)
{
Messenger.m(c.getSource(),"w term is: ",term.substring(0,1)+"b "+term);
return 1;
}
}
@@ -0,0 +1,27 @@
package carpet.mixins;
import carpet.CarpetServer;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
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.CallbackInfo;
@Mixin(CommandManager.class)
public abstract class CommandManagerMixin
{
@Shadow
@Final
private CommandDispatcher<ServerCommandSource> dispatcher;
@Inject(method = "<init>", at = @At("RETURN"))
private void onRegister(boolean boolean_1, CallbackInfo ci) {
CarpetServer.registerCarpetCommands(this.dispatcher);
}
}
+299
View File
@@ -0,0 +1,299 @@
package carpet.utils;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.ChatFormat;
import net.minecraft.network.chat.BaseComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.ClickEvent;// text.event.ClickEvent;
import net.minecraft.network.chat.HoverEvent;// text.event.HoverEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Messenger
{
public static final Logger LOG = LogManager.getLogger();
/*
messsage: "desc me ssa ge"
desc contains:
i = italic
s = strikethrough
u = underline
b = bold
o = obfuscated
w = white
y = yellow
m = magenta (light purple)
r = red
c = cyan (aqua)
l = lime (green)
t = light blue (blue)
f = dark gray
g = gray
d = gold
p = dark purple (purple)
n = dark red (brown)
q = dark aqua
e = dark green
v = dark blue (navy)
k = black
/ = action added to the previous component
*/
private static BaseComponent _applyStyleToTextComponent(BaseComponent comp, String style)
{
//could be rewritten to be more efficient
comp.getStyle().setItalic(style.indexOf('i')>=0);
comp.getStyle().setStrikethrough(style.indexOf('s')>=0);
comp.getStyle().setUnderline(style.indexOf('u')>=0);
comp.getStyle().setBold(style.indexOf('b')>=0);
comp.getStyle().setObfuscated(style.indexOf('o')>=0);
comp.getStyle().setColor(ChatFormat.WHITE);
if (style.indexOf('w')>=0) comp.getStyle().setColor(ChatFormat.WHITE); // not needed
if (style.indexOf('y')>=0) comp.getStyle().setColor(ChatFormat.YELLOW);
if (style.indexOf('m')>=0) comp.getStyle().setColor(ChatFormat.LIGHT_PURPLE);
if (style.indexOf('r')>=0) comp.getStyle().setColor(ChatFormat.RED);
if (style.indexOf('c')>=0) comp.getStyle().setColor(ChatFormat.AQUA);
if (style.indexOf('l')>=0) comp.getStyle().setColor(ChatFormat.GREEN);
if (style.indexOf('t')>=0) comp.getStyle().setColor(ChatFormat.BLUE);
if (style.indexOf('f')>=0) comp.getStyle().setColor(ChatFormat.DARK_GRAY);
if (style.indexOf('g')>=0) comp.getStyle().setColor(ChatFormat.GRAY);
if (style.indexOf('d')>=0) comp.getStyle().setColor(ChatFormat.GOLD);
if (style.indexOf('p')>=0) comp.getStyle().setColor(ChatFormat.DARK_PURPLE);
if (style.indexOf('n')>=0) comp.getStyle().setColor(ChatFormat.DARK_RED);
if (style.indexOf('q')>=0) comp.getStyle().setColor(ChatFormat.DARK_AQUA);
if (style.indexOf('e')>=0) comp.getStyle().setColor(ChatFormat.DARK_GREEN);
if (style.indexOf('v')>=0) comp.getStyle().setColor(ChatFormat.DARK_BLUE);
if (style.indexOf('k')>=0) comp.getStyle().setColor(ChatFormat.BLACK);
return comp;
}
public static String heatmap_color(double actual, double reference)
{
String color = "e";
if (actual > 0.5D*reference) color = "y";
if (actual > 0.8D*reference) color = "r";
if (actual > reference) color = "m";
return color;
}
public static String creatureTypeColor(EntityCategory type)
{
switch (type)
{
case MONSTER:
return "n";
case CREATURE:
return "e";
case AMBIENT:
return "f";
case WATER_CREATURE:
return "v";
}
return "w";
}
private static BaseComponent _getChatComponentFromDesc(String message, BaseComponent previous_message)
{
if (message.equalsIgnoreCase(""))
{
return new TextComponent("");
}
String parts[] = message.split("\\s", 2);
String desc = parts[0];
String str = "";
if (parts.length > 1) str = parts[1];
if (desc.charAt(0) == '/') // deprecated
{
if (previous_message != null)
previous_message.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message));
return previous_message;
}
if (desc.charAt(0) == '?')
{
if (previous_message != null)
previous_message.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message.substring(1)));
return previous_message;
}
if (desc.charAt(0) == '!')
{
if (previous_message != null)
previous_message.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, message.substring(1)));
return previous_message;
}
if (desc.charAt(0) == '^')
{
if (previous_message != null)
previous_message.getStyle().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, c(message.substring(1))));
return previous_message;
}
BaseComponent txt = new TextComponent(str);
return _applyStyleToTextComponent(txt, desc);
}
public static BaseComponent tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); }
public static BaseComponent tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); }
public static BaseComponent tp(String desc, double x, double y, double z) { return tp(desc, (float)x, (float)y, (float)z);}
public static BaseComponent tp(String desc, float x, float y, float z)
{
return _getCoordsTextComponent(desc, x, y, z, false);
}
public static BaseComponent tp(String desc, int x, int y, int z)
{
return _getCoordsTextComponent(desc, (float)x, (float)y, (float)z, true);
}
/// to be continued
public static BaseComponent dbl(String style, double double_value)
{
return c(String.format("%s %.1f",style,double_value),String.format("^w %f",double_value));
}
public static BaseComponent dbls(String style, double ... doubles)
{
StringBuilder str = new StringBuilder(style + " [ ");
String prefix = "";
for (double dbl : doubles)
{
str.append(String.format("%s%.1f", prefix, dbl));
prefix = ", ";
}
str.append(" ]");
return c(str.toString());
}
public static BaseComponent dblf(String style, double ... doubles)
{
StringBuilder str = new StringBuilder(style + " [ ");
String prefix = "";
for (double dbl : doubles)
{
str.append(String.format("%s%f", prefix, dbl));
prefix = ", ";
}
str.append(" ]");
return c(str.toString());
}
public static BaseComponent dblt(String style, double ... doubles)
{
List<Object> components = new ArrayList<>();
components.add(style+" [ ");
String prefix = "";
for (double dbl:doubles)
{
components.add(String.format("%s %s%.1f",style, prefix, dbl));
components.add("?"+dbl);
components.add("^w "+dbl);
prefix = ", ";
}
//components.remove(components.size()-1);
components.add(style+" ]");
return c(components.toArray(new Object[0]));
}
private static BaseComponent _getCoordsTextComponent(String style, float x, float y, float z, boolean isInt)
{
String text;
String command;
if (isInt)
{
text = String.format("%s [ %d, %d, %d ]",style, (int)x,(int)y, (int)z );
command = String.format("!/tp %d %d %d",(int)x,(int)y, (int)z);
}
else
{
text = String.format("%s [ %.1f, %.1f, %.1f]",style, x, y, z);
command = String.format("!/tp %.3f %.3f %.3f",x, y, z);
}
return c(text, command);
}
//message source
public static void m(ServerCommandSource source, Object ... fields)
{
source. sendFeedback(Messenger.c(fields),true);
}
public static void m(PlayerEntity player, Object ... fields)
{
player.sendMessage(Messenger.c(fields));
}
/*
composes single line, multicomponent message, and returns as one chat messagge
*/
public static BaseComponent c(Object ... fields)
{
BaseComponent message = new TextComponent("");
BaseComponent previous_component = null;
for (Object o: fields)
{
if (o instanceof BaseComponent)
{
message.append((BaseComponent)o);
previous_component = (BaseComponent)o;
continue;
}
String txt = o.toString();
BaseComponent comp = _getChatComponentFromDesc(txt,previous_component);
if (comp != previous_component) message.append(comp);
previous_component = comp;
}
return message;
}
//simple text
public static BaseComponent s(String text)
{
return s(text,"");
}
public static BaseComponent s(String text, String style)
{
BaseComponent message = new TextComponent(text);
_applyStyleToTextComponent(message, style);
return message;
}
public static void send(PlayerEntity player, Collection<BaseComponent> lines)
{
lines.forEach(player::sendMessage);
}
public static void send(ServerCommandSource source, Collection<BaseComponent> lines)
{
lines.stream().forEachOrdered((s) -> source.sendFeedback(s, false));
}
public static void print_server_message(MinecraftServer server, String message)
{
if (server == null)
LOG.error("Message not delivered: "+message);
server.sendMessage(new TextComponent(message));
BaseComponent txt = c("gi "+message);
for (PlayerEntity entityplayer : server.getPlayerManager().getPlayerList())
{
entityplayer.sendMessage(txt);
}
}
public static void print_server_message(MinecraftServer server, BaseComponent message)
{
if (server == null)
LOG.error("Message not delivered: "+message.getString());
server.sendMessage(message);
for (PlayerEntity entityplayer : server.getPlayerManager().getPlayerList())
{
entityplayer.sendMessage(message);
}
}
}
@@ -0,0 +1,14 @@
package net.fabricmc.example;
import net.fabricmc.api.ModInitializer;
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
System.out.println("Hello Fabric world!");
}
}
@@ -0,0 +1,15 @@
package net.fabricmc.example.mixin;
import net.minecraft.client.MinecraftClient;
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.CallbackInfo;
@Mixin(MinecraftClient.class)
public class ExampleMixin {
@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {
System.out.println("This line is printed by an example mod mixin!");
}
}