Compare commits

...

5 commits

5 changed files with 112 additions and 65 deletions

View file

@ -64,6 +64,8 @@ public class CoreCommand {
final CompletableFuture<NbtCompound> future = CommandCore.INSTANCE.runTracked(command);
future.thenApply(tag -> {
if (tag == null) return null;
try {
tag.get("LastOutput", TextCodecs.CODEC).ifPresent(source::sendFeedback);
} catch (final Exception e) {

View file

@ -27,7 +27,7 @@ public class ReloadConfigCommand {
try {
ChipmunkMod.CONFIG = ChipmunkMod.loadConfig();
CommandCore.INSTANCE.reloadRelativeArea();
CommandCore.INSTANCE.reloadFromConfig();
CustomChat.INSTANCE.reloadFromConfig();
source.sendFeedback(Text.literal("Successfully reloaded the config"));

View file

@ -25,10 +25,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.network.packet.c2s.play.UpdateCommandBlockC2SPacket;
import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket;
import net.minecraft.network.packet.c2s.play.*;
import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.state.property.Property;
@ -40,14 +37,11 @@ import net.minecraft.util.math.*;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.dimension.DimensionType;
import java.util.Collections;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import java.util.concurrent.CompletableFuture;
public class CommandCore implements Listener {
public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance());
public static final CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance());
private final MinecraftClient client;
@ -57,6 +51,7 @@ public class CommandCore implements Listener {
public BlockBox withPos;
public BlockPos block;
public boolean runFillCommand = true;
public boolean isSingleBlock = false;
public boolean alreadyFilled = false;
private final Map<BlockPos, String> pendingSetBlockCommands = Collections.synchronizedMap(new Object2ObjectArrayMap<>());
@ -67,7 +62,7 @@ public class CommandCore implements Listener {
public CommandCore (final MinecraftClient client) {
this.client = client;
reloadRelativeArea();
reloadFromConfig();
ListenerManager.addListener(this);
}
@ -97,11 +92,12 @@ public class CommandCore implements Listener {
return;
}
reloadRelativeArea();
reloadFromConfig();
}
public void reloadRelativeArea () {
public void reloadFromConfig () {
noPos = ChipmunkMod.CONFIG.core.relativeArea;
isSingleBlock = noPos.getDimensions().equals(Vec3i.ZERO);
}
@Override
@ -144,6 +140,8 @@ public class CommandCore implements Listener {
}
public void check () {
if (isSingleBlock) return;
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null || withPos == null || !ready) return;
@ -225,7 +223,7 @@ public class CommandCore implements Listener {
final ClientPlayerEntity player = client.player;
if (
!runFillCommand
!runFillCommand || isSingleBlock
|| player == null
|| !player.isInCreativeMode()
|| !player.hasPermissionLevel(2)
@ -254,7 +252,7 @@ public class CommandCore implements Listener {
networkHandler.sendChatCommand(command);
refillTriesUsingPlaceBlock = 0;
} else {
runPlaceBlock(command);
runPlaceBlock(command, true, true);
refillTriesUsingPlaceBlock++;
}
}
@ -284,8 +282,12 @@ public class CommandCore implements Listener {
public void run (final String command) {
if (command.length() > 32767) return;
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (isSingleBlock) {
runPlaceBlock(command, false, true);
return;
}
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) return;
final ClientConnection connection = networkHandler.getConnection();
@ -335,13 +337,17 @@ public class CommandCore implements Listener {
public CompletableFuture<NbtCompound> runTracked (final String command) {
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) return new CompletableFuture<>();
if (networkHandler == null) return CompletableFuture.completedFuture(null);
final ClientConnection connection = networkHandler.getConnection();
if (block == null) return new CompletableFuture<>();
if (block == null) return CompletableFuture.completedFuture(null);
if (KaboomCheck.INSTANCE.isKaboom) {
final BlockPos oldBlock;
if (isSingleBlock) {
oldBlock = runPlaceBlock(command, false, false);
} else if (KaboomCheck.INSTANCE.isKaboom) {
connection.send(
new UpdateCommandBlockC2SPacket(
block,
@ -352,6 +358,8 @@ public class CommandCore implements Listener {
true
)
);
oldBlock = block;
} else {
connection.send(
new UpdateCommandBlockC2SPacket(
@ -374,17 +382,24 @@ public class CommandCore implements Listener {
true
)
);
oldBlock = block;
}
if (oldBlock == null) return CompletableFuture.completedFuture(null);
final CompletableFuture<NbtCompound> future = new CompletableFuture<>();
final Timer timer = new Timer();
final BlockPos oldBlock = block;
final TimerTask queryTask = new TimerTask() {
public void run () {
client.getNetworkHandler().getDataQueryHandler().queryBlockNbt(oldBlock, future::complete);
client.getNetworkHandler().getDataQueryHandler().queryBlockNbt(oldBlock, result -> {
future.complete(result);
if (isSingleBlock && client.interactionManager != null) {
client.interactionManager.breakBlock(oldBlock);
}
});
}
};
@ -395,21 +410,21 @@ public class CommandCore implements Listener {
return future;
}
public void runPlaceBlock (final String command) {
public BlockPos runPlaceBlock (final String command, final boolean fallbackToChat, final boolean replaceOldBlock) {
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) return;
if (networkHandler == null) return null;
final ClientConnection connection = networkHandler.getConnection();
final ClientPlayerEntity player = client.player;
final ClientWorld world = client.world;
if (player == null || world == null || !player.isInCreativeMode()) return;
if (player == null || world == null || !player.isInCreativeMode()) return null;
final DimensionType dimensionType = world.getDimension();
final ClientPlayerInteractionManager interactionManager = client.interactionManager;
if (interactionManager == null) return;
if (interactionManager == null) return null;
// stolen from two five hundred million dollars
@ -420,8 +435,8 @@ public class CommandCore implements Listener {
|| pair.getLeft().getY() < dimensionType.minY()
|| pair.getLeft().getY() > dimensionType.height() + dimensionType.minY()
) {
networkHandler.sendChatCommand(command);
return;
if (fallbackToChat) networkHandler.sendChatCommand(command);
return null;
}
final BlockPos position = pair.getLeft();
@ -455,8 +470,15 @@ public class CommandCore implements Listener {
connection.send(new UpdateSelectedSlotC2SPacket(freeHotBarSlot));
}
interactionManager.breakBlock(position);
try (final PendingUpdateManager pendingUpdateManager = ((ClientWorldAccessor) world).getPendingUpdateManager()) {
connection.send(
new PlayerActionC2SPacket(
PlayerActionC2SPacket.Action.START_DESTROY_BLOCK,
position,
Direction.UP,
pendingUpdateManager.incrementSequence().getSequence()
)
);
connection.send(
new PlayerInteractBlockC2SPacket(
Hand.MAIN_HAND,
@ -469,39 +491,56 @@ public class CommandCore implements Listener {
)
);
}
interactionManager.interactBlock(
player,
Hand.MAIN_HAND,
new BlockHitResult(
new Vec3d(position),
Direction.UP,
position,
false
)
);
if (oldSelectedSlot != freeHotBarSlot) {
connection.send(new UpdateSelectedSlotC2SPacket(oldSelectedSlot));
}
connection.send(new CreativeInventoryActionC2SPacket(slot, oldStack));
final StringBuilder oldBlockString = new StringBuilder(Registries.BLOCK.getId(oldBlockState.getBlock()).toString());
if (!oldBlockState.getProperties().isEmpty()) {
oldBlockString.append('[');
if (ChipmunkMod.CONFIG.core.logCommands)
ChipmunkMod.LOGGER.info("Executing place block core command: {}", command);
for (final Property<?> property : oldBlockState.getProperties()) {
oldBlockString.append(property.getName())
.append('=')
.append(Util.getValueAsString(property, property.getValues().getLast())) // water[level=15] instead of water[level=0,level=1,....,level=15]
.append(',');
if (!replaceOldBlock) return position;
if (isSingleBlock) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run () {
// we're gonna just only break the block for now
try (final PendingUpdateManager pendingUpdateManager = ((ClientWorldAccessor) world).getPendingUpdateManager()) {
connection.send(
new PlayerActionC2SPacket(
PlayerActionC2SPacket.Action.START_DESTROY_BLOCK,
position,
Direction.UP,
pendingUpdateManager.incrementSequence().getSequence()
)
);
}
}
}, 50 * 2);
} else {
final StringBuilder oldBlockString = new StringBuilder(Registries.BLOCK.getId(oldBlockState.getBlock()).toString());
if (!oldBlockState.getProperties().isEmpty()) {
oldBlockString.append('[');
for (final Property<?> property : oldBlockState.getProperties()) {
oldBlockString.append(property.getName())
.append('=')
.append(Util.getValueAsString(property, property.getValues().getLast())) // water[level=15] instead of water[level=0,level=1,....,level=15]
.append(',');
}
oldBlockString.deleteCharAt(oldBlockString.length() - 1);
oldBlockString.append(']');
}
oldBlockString.deleteCharAt(oldBlockString.length() - 1);
oldBlockString.append(']');
pendingSetBlockCommands.put(position, oldBlockString.toString());
}
pendingSetBlockCommands.put(position, oldBlockString.toString());
return position;
}
// also from two five hundred million dollars

View file

@ -14,6 +14,7 @@ import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import java.io.File;
@ -50,6 +51,7 @@ public class SongPlayer {
}
// TODO: Less duplicate code
// TODO: Reduce the mess
public void loadSong (final Path location) {
final ClientPlayerEntity player = client.player;
@ -108,10 +110,12 @@ public class SongPlayer {
if (loaderThread != null && !loaderThread.isAlive() && client.player != null) {
if (loaderThread.exception != null) {
client.player.sendMessage(Component.translatable("Failed to load song: %s", Component.text(loaderThread.exception.message.getString())).color(NamedTextColor.RED));
final Text exceptionMessage = loaderThread.exception.message;
client.execute(() -> client.player.sendMessage(Component.translatable("Failed to load song: %s", Component.text(exceptionMessage.getString())).color(NamedTextColor.RED)));
} else {
songQueue.add(loaderThread.song);
client.player.sendMessage(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.DARK_GREEN)).color(NamedTextColor.GREEN));
final Component name = loaderThread.song.name;
client.execute(() -> client.player.sendMessage(Component.translatable("Added %s to the song queue", Component.empty().append(name).color(NamedTextColor.DARK_GREEN)).color(NamedTextColor.GREEN)));
}
loaderThread = null;
}
@ -120,16 +124,18 @@ public class SongPlayer {
if (songQueue.isEmpty()) return;
currentSong = songQueue.poll();
if (client.player != null) client.player.sendMessage(
Component
.translatable(
"Now playing %s",
Component.empty()
.append(currentSong.name)
.color(NamedTextColor.DARK_GREEN)
)
.color(NamedTextColor.GREEN)
);
if (client.player != null) {
client.execute(() -> client.player.sendMessage(
Component
.translatable(
"Now playing %s",
Component.empty()
.append(currentSong.name)
.color(NamedTextColor.DARK_GREEN)
)
.color(NamedTextColor.GREEN)
));
}
currentSong.play();
}
@ -145,7 +151,7 @@ public class SongPlayer {
e.printStackTrace();
}
if (currentSong.paused) return;
if (currentSong == null || currentSong.paused) return;
handlePlaying();

View file

@ -20,5 +20,5 @@ public class UUIDUtilities {
return "[I;" + array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "]"; // TODO: improve lol
}
public static String selector (final UUID uuid) { return "@a[limit=1,nbt={UUID:" + snbt(uuid) + "}]"; }
public static String selector (final UUID uuid) { return "@p[nbt={UUID:" + snbt(uuid) + "}]"; }
}