Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes mixins and player.sendChatMessage #350

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
public class RedstoneToolsClient implements ClientModInitializer {

public static final String MOD_ID = "redstonetools";
public static final String MOD_VERSION = "v" + FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata().getVersion().getFriendlyString();
public static final String MOD_VERSION = "v" + FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow()
.getMetadata().getVersion().getFriendlyString();
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final Path CONFIG_DIR = FabricLoader.getInstance().getConfigDir().resolve("redstonetools");
public static final Injector INJECTOR = Doctor.createInjector(ReflectionUtils.getModules());
Expand All @@ -32,7 +33,8 @@ public void onInitializeClient() {
LOGGER.trace("Registering feature {}", feature.getClass().getName());

if (feature.requiresWorldEdit() && !DependencyLookup.WORLDEDIT_PRESENT) {
LOGGER.warn("Feature {} requires WorldEdit, but WorldEdit is not loaded. Skipping registration.", feature.getName());
LOGGER.warn("Feature {} requires WorldEdit, but WorldEdit is not loaded. Skipping registration.",
feature.getName());
return;
}
feature.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void register() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) {
assert client.player != null;
client.player.sendMessage(Text.literal("/" + info.command()));
client.player.networkHandler.sendChatCommand(info.command());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void register() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) {
assert client.player != null;
client.player.sendChatMessage("/" + info.command());
client.player.networkHandler.sendChatCommand(info.command());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void run() {
var player = MinecraftClient.getInstance().player;
assert player != null;

player.sendChatMessage(command.startsWith("/") ? command : "/" + command);
player.networkHandler.sendChatCommand(command.startsWith("/") ? command.substring(1) : command);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ private AutoDustFeature getAutoDustFeature() {
}

@Inject(method = "onPlaced", at = @At("TAIL"))
private void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack, CallbackInfo ci) {
private void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack,
CallbackInfo ci) {
if (!getAutoDustFeature().isEnabled()) {
return;
}
Expand All @@ -48,7 +49,9 @@ private void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity
return;
}

ItemPlacementContext context = new ItemPlacementContext((PlayerEntity) placer, Hand.MAIN_HAND,new ItemStack(Items.REDSTONE),new BlockHitResult(new Vec3d(dustPos.getX(),dustPos.getY(),dustPos.getZ()), Direction.UP, dustPos,false));
placer.method_48926().setBlockState(dustPos, Blocks.REDSTONE_WIRE.getPlacementState(context));
ItemPlacementContext context = new ItemPlacementContext((PlayerEntity) placer, Hand.MAIN_HAND,
new ItemStack(Items.REDSTONE), new BlockHitResult(
new Vec3d(dustPos.getX(), dustPos.getY(), dustPos.getZ()), Direction.UP, dustPos, false));
placer.getWorld().setBlockState(dustPos, Blocks.REDSTONE_WIRE.getPlacementState(context));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.redstone.redstonetools.mixin.features;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -25,7 +26,6 @@

import static tools.redstone.redstonetools.RedstoneToolsClient.INJECTOR;


public abstract class ItemBindMixin {

@Mixin(ItemStack.class)
Expand All @@ -35,7 +35,8 @@ private abstract static class ItemStackMixin {
public abstract @Nullable NbtCompound getNbt();

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
public void checkCommandNBT(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
public void checkCommandNBT(World world, PlayerEntity user, Hand hand,
CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
if (tryToExecuteNBTCommand(hand, world)) {
cir.setReturnValue(TypedActionResult.pass((ItemStack) ((Object) this)));
}
Expand All @@ -49,30 +50,33 @@ public void checkCommandNBT(ItemUsageContext context, CallbackInfoReturnable<Act
}

private boolean tryToExecuteNBTCommand(Hand hand, World world) {
if (hand == Hand.OFF_HAND || world.isClient) return false;
if (hand == Hand.OFF_HAND || world.isClient)
return false;
NbtCompound nbt = getNbt();
if (nbt == null || !nbt.contains("command")) return false;
if (nbt == null || !nbt.contains("command"))
return false;
NbtString command = (NbtString) nbt.get("command");
MinecraftClient.getInstance().player.sendChatMessage(command.asString());
MinecraftClient.getInstance().player.networkHandler.sendChatCommand(command.asString());

return true;
}
}

@Mixin(ClientPlayerEntity.class)
private abstract static class PlayerMixin {
@Mixin(ClientPlayNetworkHandler.class)
private abstract static class NetworkHandlerMixin {

@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
@Inject(method = "sendChatCommand", at = @At("HEAD"), cancellable = true)
public void injectCommand(String message, CallbackInfo ci) {
if (!message.startsWith("/") || !ItemBindFeature.waitingForCommand) return;
if (!ItemBindFeature.waitingForCommand)
return;

Feedback addCommandFeedback = ItemBindFeature.addCommand(message);
if (addCommandFeedback != null) {
INJECTOR.getInstance(FeedbackSender.class).sendFeedback(((Entity) ((Object)this)).getCommandSource(),addCommandFeedback);
INJECTOR.getInstance(FeedbackSender.class).sendFeedback(((Entity) ((Object) this)).getCommandSource(),
addCommandFeedback);
ci.cancel();
}
}
}


}
2 changes: 1 addition & 1 deletion src/main/resources/redstonetools.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"accessors.MinecraftClientAccessor",
"accessors.WorldRendererAccessor",
"features.AirPlaceClientMixin",
"features.ItemBindMixin$PlayerMixin",
"features.ItemBindMixin$NetworkHandlerMixin",
"macros.AddMacroButtonMixin",
"macros.InitializeMacroManagerMixin",
"macros.KeyBindingMixinImpl",
Expand Down
Loading