Skip to content

Commit

Permalink
0.6.10
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Dec 15, 2024
1 parent a862b52 commit f9dd64c
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 222 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Sign Me Up
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=BSD-3-Clause
# The mod version. See https://semver.org/
mod_version=0.6.9
mod_version=0.6.10
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
22 changes: 0 additions & 22 deletions run/config/SignMeUp/Waypoints.json

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/java/org/teacon/signmeup/SignMeUp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.teacon.signmeup;

import cn.ussshenzhou.t88.config.ConfigHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
Expand All @@ -11,7 +10,6 @@
import org.teacon.signmeup.config.Map;
import org.teacon.signmeup.config.MiniMap;
import org.teacon.signmeup.config.PlayerCommands;
import org.teacon.signmeup.config.Waypoints;

/**
* @author USS_Shenzhou
Expand All @@ -28,7 +26,6 @@ public SignMeUp(IEventBus bus, ModContainer mod) {

ConfigHelper.loadConfig(new PlayerCommands());
ConfigHelper.loadConfig(new Map());
ConfigHelper.loadConfig(new Waypoints());
ConfigHelper.loadConfig(new MiniMap());
}

Expand Down
59 changes: 28 additions & 31 deletions src/main/java/org/teacon/signmeup/command/OpCommands.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.teacon.signmeup.command;

import cn.ussshenzhou.t88.config.ConfigHelper;
import cn.ussshenzhou.t88.network.NetworkHelper;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -13,16 +12,13 @@
import net.minecraft.commands.arguments.coordinates.RotationArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.commands.arguments.coordinates.WorldCoordinates;
import net.minecraft.core.Rotations;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import org.teacon.signmeup.command.argument.SpaceBreakStringArgumentType;
import org.teacon.signmeup.config.Waypoints;
import org.teacon.signmeup.config.waypoints.Waypoint;
import org.teacon.signmeup.network.RemoveWaypointPacket;
import org.teacon.signmeup.network.SetWaypointPacket;

import java.util.Optional;

/**
* @author USS_Shenzhou
*/
Expand All @@ -46,8 +42,8 @@ public static void waypoints(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(Commands.literal("remove")
.then(Commands.argument("name", SpaceBreakStringArgumentType.string())
.suggests((context, builder) -> SharedSuggestionProvider.suggest(
ConfigHelper.getConfigRead(Waypoints.class).waypoints.stream().map(wayPoint -> wayPoint.name), builder))
.executes(OpCommands::removeWaypoint)
Waypoint.INSTANCES.keySet(), builder
)).executes(OpCommands::removeWaypoint)
)
)
);
Expand All @@ -58,37 +54,38 @@ private static int setWaypoint(CommandContext<CommandSourceStack> context) {
var name = context.getArgument("name", String.class);
var description = context.getArgument("description", String.class);
var rotation = context.getArgument("rotation", WorldCoordinates.class).getRotation(context.getSource());
var waypoint = new Waypoints.WayPoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.y, rotation.x);
var waypoint = new Waypoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.y, rotation.x);

Waypoint previous = Waypoint.INSTANCES.get(name);
if (previous != null) {
context.getSource().sendSuccess(() -> Component.literal(previous + " already exists. Remove it first if you want to replace it."), true);
} else {
Waypoint.INSTANCES.put(name, waypoint);
NetworkHelper.sendToAllPlayers(new SetWaypointPacket(waypoint));

context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been added."), true);
if (description.startsWith("\"") && description.endsWith("\"")) {
context.getSource().sendSuccess(() -> Component.literal("The waypoint description is quoted with '\"'. This is a greedy string where '\"' is unnecessary.").setStyle(Style.EMPTY.withColor(ChatFormatting.YELLOW)), true);
}
}

ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> {
waypoints.waypoints.stream().filter(w -> w.name.equals(waypoint.name)).findFirst().ifPresentOrElse(
point -> context.getSource().sendSuccess(() -> Component.literal(point + " already exists. Remove it first if you want to replace it."), true),
() -> {
waypoints.waypoints.add(waypoint);
NetworkHelper.sendToAllPlayers(new SetWaypointPacket(name, description, pos, new Rotations(rotation.y, 0, rotation.x)));
context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been added."), true);
if (description.startsWith("\"") && description.endsWith("\"")) {
context.getSource().sendSuccess(() -> Component.literal("The waypoint description is quoted with '\"'. This is a greedy string where '\"' is unnecessary.").setStyle(Style.EMPTY.withColor(ChatFormatting.YELLOW)), true);
}
}
);
});
Waypoint.save();

return Command.SINGLE_SUCCESS;
}

private static int removeWaypoint(CommandContext<CommandSourceStack> context) {
var name = context.getArgument("name", String.class);
var waypoint = Waypoints.WayPoint.dumbWayPoint(name);
ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> {
if (waypoints.waypoints.remove(waypoint)) {
context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been removed."), true);
Optional.ofNullable(context.getSource().getPlayer())
.ifPresent(player -> NetworkHelper.sendToAllPlayers(new RemoveWaypointPacket(name)));
} else {
context.getSource().sendFailure(Component.literal("No waypoint called " + name));
}
});
Waypoint waypoint = Waypoint.INSTANCES.remove(name);

if (waypoint != null) {
context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been removed."), true);
NetworkHelper.sendToAllPlayers(new RemoveWaypointPacket(name));
} else {
context.getSource().sendFailure(Component.literal("No waypoint called " + name));
}

Waypoint.save();
return Command.SINGLE_SUCCESS;
}
}
60 changes: 0 additions & 60 deletions src/main/java/org/teacon/signmeup/config/Waypoints.java

This file was deleted.

83 changes: 83 additions & 0 deletions src/main/java/org/teacon/signmeup/config/waypoints/Waypoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.teacon.signmeup.config.waypoints;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class Waypoint {
public static final Map<String, Waypoint> INSTANCES = new ConcurrentHashMap<>();

public static final StreamCodec<ByteBuf, Waypoint> CODEC = new StreamCodec<>() {
@Override
public @NotNull Waypoint decode(@NotNull ByteBuf buffer) {
return new Waypoint(
ByteBufCodecs.STRING_UTF8.decode(buffer),
ByteBufCodecs.STRING_UTF8.decode(buffer),
ByteBufCodecs.VAR_INT.decode(buffer),
ByteBufCodecs.VAR_INT.decode(buffer),
ByteBufCodecs.VAR_INT.decode(buffer),
ByteBufCodecs.FLOAT.decode(buffer),
ByteBufCodecs.FLOAT.decode(buffer)
);
}

@Override
public void encode(@NotNull ByteBuf buffer, @NotNull Waypoint value) {
ByteBufCodecs.STRING_UTF8.encode(buffer, value.name);
ByteBufCodecs.STRING_UTF8.encode(buffer, value.description);
ByteBufCodecs.VAR_INT.encode(buffer, value.x);
ByteBufCodecs.VAR_INT.encode(buffer, value.y);
ByteBufCodecs.VAR_INT.encode(buffer, value.z);
ByteBufCodecs.FLOAT.encode(buffer, value.rx);
ByteBufCodecs.FLOAT.encode(buffer, value.ry);
}
};

public static void load() {
for (Waypoint waypoint : WaypointSavedData.load()) {
INSTANCES.put(waypoint.name, waypoint);
}
}

public static void save() {
WaypointSavedData.save(INSTANCES.values());
}

public final String name, description;
public final int x, y, z;

public final float rx, ry;

public Waypoint(String name, String description, int x, int y, int z, float rx, float ry) {
this.name = name;
this.description = description;
this.x = x;
this.y = y;
this.z = z;
this.rx = rx;
this.ry = ry;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Waypoint that) {
return Objects.equals(that.name, name);
}
return false;
}

@Override
public int hashCode() {
return name.hashCode();
}

@Override
public String toString() {
return "[WayPoint name=" + name + ", description=" + description + ", <" + x + ", " + y + ", " + z + ">, rotation=<" + rx + ", " + ry + ">]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.teacon.signmeup.config.waypoints;

import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import org.teacon.signmeup.SignMeUp;

@EventBusSubscriber(value = Dist.CLIENT, modid = SignMeUp.MODID, bus = EventBusSubscriber.Bus.GAME)
public class WaypointClientEvents {
@SubscribeEvent
public static void onPlayerLoggedOut(ClientPlayerNetworkEvent.LoggingOut event) {
Waypoint.INSTANCES.clear();
}
}
Loading

0 comments on commit f9dd64c

Please sign in to comment.