Skip to content

Commit

Permalink
simplified interaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
daviirodrig committed Jun 16, 2024
1 parent 57ea0d2 commit bcde016
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.mitsuaky.stanleyparable.common.network.packets;

import com.mitsuaky.stanleyparable.common.network.PacketHandler;
import com.mitsuaky.stanleyparable.server.commands.InteractionCheckCommand;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.event.network.CustomPayloadEvent;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class PacketInteractionToServer implements Packet {

private final String interactionType;
Expand All @@ -32,11 +27,7 @@ public void toBytes(FriendlyByteBuf buf) {

@Override
public void handle(CustomPayloadEvent.Context ctx) {
ctx.enqueueWork(() -> {
InteractionCheckCommand.interactionMap.put(interactionType, interactionValue);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(() -> InteractionCheckCommand.interactionMap.put(interactionType, false), 3, TimeUnit.SECONDS);
});
ctx.enqueueWork(() -> InteractionCheckCommand.interactionMap.put(interactionType, interactionValue));
ctx.setPacketHandled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.mitsuaky.stanleyparable.server.commands.InteractionCheckCommand.interactionMap;

public class ForceInteractionCommand {
static ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal("forceinteraction")
.then(Commands.argument("interactionType", StringArgumentType.word())
.executes(ForceInteractionCommand::runCmdOnOff)
.then(Commands.argument("interactionValue", BoolArgumentType.bool())
.executes(ForceInteractionCommand::runCmdForced)));
}
Expand All @@ -31,13 +26,4 @@ private static int runCmdForced(CommandContext<CommandSourceStack> ctx) {
ctx.getSource().sendSuccess(() -> Component.literal("Interaction " + interactionType + " setado para " + interactionValue), false);
return 1;
}

private static int runCmdOnOff(CommandContext<CommandSourceStack> ctx) {
String interactionType = StringArgumentType.getString(ctx, "interactionType");
interactionMap.put(interactionType, true);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(() -> interactionMap.put(interactionType, false), 3, TimeUnit.SECONDS);
ctx.getSource().sendSuccess(() -> Component.literal("Interaction " + interactionType + " ativada, daqui 3 segundos irá desativar."), false);
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class InteractionCheckCommand {
private static int runCmd(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
String interactionType = StringArgumentType.getString(ctx, "interactionType");

if (interactionMap.containsKey(interactionType) && interactionMap.get(interactionType)) {
if (interactionMap.get(interactionType)) {
if (StanleyParableMod.debugMode) {
ctx.getSource().sendSuccess(() -> Component.literal("Interaction detected"), true);
ctx.getSource().getServer().getPlayerList().broadcastSystemMessage(Component.literal("Interaction " + interactionType + " detected"), false);
}
ctx.getSource().sendSuccess(() -> Component.literal("Interaction detected"), false);
interactionMap.put(interactionType, false);
return 1;
} else {
if (StanleyParableMod.debugMode) {
ctx.getSource().sendFailure(Component.literal("Interaction not detected"));
}
ctx.getSource().sendFailure(Component.literal("Interaction " + interactionType + " not detected"));
throw new SimpleCommandExceptionType(Component.translatable("commands.execute.conditional.fail")).create();
}
}
Expand Down

0 comments on commit bcde016

Please sign in to comment.