Skip to content

Commit

Permalink
Merge pull request #4 from fewwan/configurable-radius
Browse files Browse the repository at this point in the history
Added configurable radius
  • Loading branch information
IkyMax authored Dec 30, 2024
2 parents 1cb4737 + b715c6c commit b5eb280
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 46 deletions.
126 changes: 95 additions & 31 deletions src/main/java/de/guntram/mcmod/antighost/AntiGhost.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package de.guntram.mcmod.antighost;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import de.guntram.mcmod.crowdintranslate.CrowdinTranslate;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
Expand All @@ -14,54 +16,116 @@
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

import static org.lwjgl.glfw.GLFW.GLFW_KEY_G;

public class AntiGhost implements ClientModInitializer
{
static final String MODID="antighost";
static KeyBinding requestBlocks;

public class AntiGhost implements ClientModInitializer {
public static final String MOD_ID = "antighost";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve(MOD_ID + ".properties");
private static KeyBinding requestBlocksKey;
private static int radius = 4; // default radius

@Override
public void onInitializeClient()
{
final String category="key.categories.antighost";
requestBlocks = new KeyBinding("key.antighost.reveal", GLFW_KEY_G, category);
CrowdinTranslate.downloadTranslations(MODID);
KeyBindingHelper.registerKeyBinding(requestBlocks);
ClientTickEvents.END_CLIENT_TICK.register(e->keyPressed());
public void onInitializeClient() {
loadConfig();
final String category = "key.categories.antighost";
requestBlocksKey = new KeyBinding("key.antighost.reveal", GLFW_KEY_G, category);
CrowdinTranslate.downloadTranslations(MOD_ID);
KeyBindingHelper.registerKeyBinding(requestBlocksKey);
ClientTickEvents.END_CLIENT_TICK.register(e -> keyPressed());
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
dispatcher.register(
ClientCommandManager.literal("ghost").executes(c -> {
this.execute();
return 0;
})
// /ghost [radius]
dispatcher.register(ClientCommandManager.literal("ghost")
.then(ClientCommandManager.argument("radius", IntegerArgumentType.integer(1))
.executes(c -> {
int customRadius = IntegerArgumentType.getInteger(c, "radius");
this.requestBlocks(customRadius);
return 0;
})
)
// with configured radius
.executes(c -> {
this.requestBlocks(radius);
return 0;
})
);

// /antighost setradius <radius>
dispatcher.register(ClientCommandManager.literal("antighost")
.then(ClientCommandManager.literal("setradius")
.then(ClientCommandManager.argument("radius", IntegerArgumentType.integer(1))
.executes(c -> {
radius = IntegerArgumentType.getInteger(c, "radius");
saveConfig();
ClientPlayerEntity player = MinecraftClient.getInstance().player;
player.sendMessage(Text.translatable("msg.radius_set", radius), false);
return 0;
})
)
)
);
});
}

public void keyPressed() {
ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (requestBlocks.wasPressed()) {
this.execute();
if (requestBlocksKey.wasPressed()) {
this.requestBlocks(radius);
player.sendMessage(Text.translatable("msg.request"), false);
}
}
public void execute() {
MinecraftClient mc=MinecraftClient.getInstance();

public void requestBlocks(int radius) {
MinecraftClient mc = MinecraftClient.getInstance();
ClientPlayNetworkHandler conn = mc.getNetworkHandler();
if (conn==null)
return;
BlockPos pos=mc.player.getBlockPos();
for (int dx=-4; dx<=4; dx++)
for (int dy=-4; dy<=4; dy++)
for (int dz=-4; dz<=4; dz++) {
PlayerActionC2SPacket packet=new PlayerActionC2SPacket(
PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK,
new BlockPos(pos.getX()+dx, pos.getY()+dy, pos.getZ()+dz),
if (conn == null) return;
BlockPos pos = mc.player.getBlockPos();
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
for (int dz = -radius; dz <= radius; dz++) {
PlayerActionC2SPacket packet = new PlayerActionC2SPacket(
PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK,
new BlockPos(pos.getX() + dx, pos.getY() + dy, pos.getZ() + dz),
Direction.UP // with ABORT_DESTROY_BLOCK, this value is unused
);
conn.sendPacket(packet);
}
}
}
}

private void loadConfig() {
Properties properties = new Properties();

if (Files.exists(CONFIG_PATH)) {
try (var reader = Files.newBufferedReader(CONFIG_PATH)) {
properties.load(reader);
radius = Integer.parseInt(properties.getProperty("radius", String.valueOf(radius)));
} catch (IOException | NumberFormatException e) {
LOGGER.error("Failed to load configuration. Using default radius.", e);
}
} else {
saveConfig();
}
}

private void saveConfig() {
Properties properties = new Properties();
properties.setProperty("radius", String.valueOf(radius));

try (var writer = Files.newBufferedWriter(CONFIG_PATH)) {
Files.createDirectories(CONFIG_PATH.getParent());
properties.store(writer, null);
} catch (IOException e) {
LOGGER.error("Failed to save configuration.", e);
}
}
}
7 changes: 4 additions & 3 deletions src/main/resources/assets/antighost/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiGhost",
"key.antighost.reveal": "Reveal ghost blocks",
"msg.request": "requesting resend of blocks around you"
"key.categories.antighost": "AntiGhost",
"key.antighost.reveal": "Reveal ghost blocks",
"msg.request": "requesting resend of blocks around you",
"msg.radius_set": "radius has been set to %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_ar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_cl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_ec.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_es.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_mx.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_uy.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/antighost/lang/es_ve.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"key.categories.antighost": "AntiFantasma",
"key.antighost.reveal": "Mostrar bloques fantasmas",
"msg.request": "Solicitando reenvío de bloques a tu alrededor"
"msg.request": "Solicitando reenvío de bloques a tu alrededor",
"msg.radius_set": "el radio se ha establecido en %d."
}
10 changes: 5 additions & 5 deletions src/main/resources/assets/antighost/lang/tt_ru.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"modmenu.summaryTranslation.antighost": "Сезне ялган блоклардан чыгарга ярдәм итүче мод.",
"modmenu.descriptionTranslation.antighost": "Сезне ялган блоклардан чыгарга ярдәм итүче мод. Блоклар яңартуының соравын җибәрү өчен «/ghost» боерыгын кулланыгыз яки «g» төймәсенә (яңадан билгеләргә мөмкин) басыгыз.",
"key.categories.antighost": "AntiGhost",
"key.antighost.reveal": "Үз тирәсендә блокларны яңарту",
"msg.request": "§9Блоклар яңартуын сорау җиберелде."
"modmenu.summaryTranslation.antighost": "Сезне ялган блоклардан чыгарга ярдәм итүче мод.",
"modmenu.descriptionTranslation.antighost": "Сезне ялган блоклардан чыгарга ярдәм итүче мод. Блоклар яңартуының соравын җибәрү өчен «/ghost» боерыгын кулланыгыз яки «g» төймәсенә (яңадан билгеләргә мөмкин) басыгыз.",
"key.categories.antighost": "AntiGhost",
"key.antighost.reveal": "Үз тирәсендә блокларны яңарту",
"msg.request": "§9Блоклар яңартуын сорау җиберелде."
}

0 comments on commit b5eb280

Please sign in to comment.