From 88860f4dc68c2d7321e742c3326b8ca1f0263503 Mon Sep 17 00:00:00 2001 From: DancingSnow <1121149616@qq.com> Date: Sun, 9 Apr 2023 22:16:04 +0800 Subject: [PATCH] build(curtain): Update mod version to 1.2.1 --- gradle.properties | 2 +- .../java/dev/dubhe/curtain/CurtainRules.java | 2 +- src/main/java/dev/dubhe/curtain/ICurtain.java | 1 - .../dev/dubhe/curtain/api/rules/Rule.java | 8 +- .../dubhe/curtain/api/rules/Validators.java | 6 +- .../dubhe/curtain/commands/PlayerCommand.java | 6 +- .../dubhe/curtain/commands/RuleCommand.java | 11 +- .../PlayerLoggedEventHandler.java | 7 +- .../PlayerTickEventHandler.java | 6 +- .../entityInteractHandler.java | 13 +- .../features/logging/AbstractLogger.java | 4 +- .../features/logging/LoggerManager.java | 7 +- .../logging/builtin/MemoryLogger.java | 2 +- .../logging/helper/ExplosionLogHelper.java | 1 - .../features/player/fakes/IEntity.java | 1 + .../features/player/fakes/IServerPlayer.java | 3 + .../helpers/EntityPlayerActionPack.java | 475 ++++++++---------- .../player/menu/FakePlayerInventoryMenu.java | 76 +-- .../features/player/menu/MenuHashMap.java | 2 +- .../player/menu/component/Button.java | 35 +- .../menu/component/RadioButtonPanel.java | 34 +- .../player/menu/component/ToggledButton.java | 36 +- .../player/patches/EntityPlayerMPFake.java | 40 +- .../player/patches/FakeClientConnection.java | 2 +- .../dev/dubhe/curtain/mixins/EntityMixin.java | 13 +- .../curtain/mixins/ServerPlayerMixin.java | 13 +- .../AbstractContainerMenuMixin.java | 18 +- .../rules/missing_tools/PickaxeItemMixin.java | 6 +- .../AbstractContainerMenuMixin.java | 4 +- .../xp_no_cooldown/ExperienceOrbMixin.java | 5 +- .../rules/xp_no_cooldown/PlayerMixin.java | 2 +- .../dubhe/curtain/utils/CommandHelper.java | 18 +- .../dev/dubhe/curtain/utils/MenuHelper.java | 12 +- .../dev/dubhe/curtain/utils/Messenger.java | 263 +++++----- .../java/dev/dubhe/curtain/utils/Tracer.java | 50 +- 35 files changed, 562 insertions(+), 622 deletions(-) diff --git a/gradle.properties b/gradle.properties index 578d4ed..01b0de6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,5 +7,5 @@ minecraft_version=1.19.2 # Mod Info maven_group=dev.dubhe archives_base_name=curtain -version=1.2.0 +version=1.2.1 build_number = undefined diff --git a/src/main/java/dev/dubhe/curtain/CurtainRules.java b/src/main/java/dev/dubhe/curtain/CurtainRules.java index 4c15a91..0c90e2a 100644 --- a/src/main/java/dev/dubhe/curtain/CurtainRules.java +++ b/src/main/java/dev/dubhe/curtain/CurtainRules.java @@ -1,8 +1,8 @@ package dev.dubhe.curtain; import dev.dubhe.curtain.api.rules.CurtainRule; -import dev.dubhe.curtain.api.rules.Rule; import dev.dubhe.curtain.api.rules.IValidator; +import dev.dubhe.curtain.api.rules.Rule; import dev.dubhe.curtain.api.rules.Validators; import dev.dubhe.curtain.utils.TranslationHelper; import net.minecraft.commands.CommandSourceStack; diff --git a/src/main/java/dev/dubhe/curtain/ICurtain.java b/src/main/java/dev/dubhe/curtain/ICurtain.java index 2076682..aa27b23 100644 --- a/src/main/java/dev/dubhe/curtain/ICurtain.java +++ b/src/main/java/dev/dubhe/curtain/ICurtain.java @@ -4,7 +4,6 @@ import dev.dubhe.curtain.utils.TranslationHelper; import java.io.InputStream; -import java.io.Reader; public interface ICurtain { diff --git a/src/main/java/dev/dubhe/curtain/api/rules/Rule.java b/src/main/java/dev/dubhe/curtain/api/rules/Rule.java index 9e2f565..757a9d8 100644 --- a/src/main/java/dev/dubhe/curtain/api/rules/Rule.java +++ b/src/main/java/dev/dubhe/curtain/api/rules/Rule.java @@ -1,13 +1,19 @@ package dev.dubhe.curtain.api.rules; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Rule { String[] categories(); + Class>[] validators() default {}; + String[] suggestions(); String serializedName() default ""; diff --git a/src/main/java/dev/dubhe/curtain/api/rules/Validators.java b/src/main/java/dev/dubhe/curtain/api/rules/Validators.java index 2bdab79..62fe535 100644 --- a/src/main/java/dev/dubhe/curtain/api/rules/Validators.java +++ b/src/main/java/dev/dubhe/curtain/api/rules/Validators.java @@ -6,14 +6,16 @@ import java.util.List; public final class Validators { - private Validators(){} + private Validators() { + } public static class CommandLevel implements IValidator { public static final List OPTIONS = List.of("true", "false", "ops", "0", "1", "2", "3", "4"); + @Override public boolean validate(CommandSourceStack source, CurtainRule rule, String newValue) { boolean is_valid = OPTIONS.contains(newValue); - if(source!=null && is_valid) + if (source != null && is_valid) CommandHelper.notifyPlayersCommandsChanged(source.getServer()); return is_valid; } diff --git a/src/main/java/dev/dubhe/curtain/commands/PlayerCommand.java b/src/main/java/dev/dubhe/curtain/commands/PlayerCommand.java index d137e22..b0a4c1b 100644 --- a/src/main/java/dev/dubhe/curtain/commands/PlayerCommand.java +++ b/src/main/java/dev/dubhe/curtain/commands/PlayerCommand.java @@ -33,7 +33,11 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import java.util.function.Consumer; import static net.minecraft.commands.Commands.argument; diff --git a/src/main/java/dev/dubhe/curtain/commands/RuleCommand.java b/src/main/java/dev/dubhe/curtain/commands/RuleCommand.java index 4f7606d..86fefcf 100644 --- a/src/main/java/dev/dubhe/curtain/commands/RuleCommand.java +++ b/src/main/java/dev/dubhe/curtain/commands/RuleCommand.java @@ -1,7 +1,12 @@ package dev.dubhe.curtain.commands; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.*; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.FloatArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import dev.dubhe.curtain.Curtain; @@ -19,7 +24,9 @@ import net.minecraft.network.chat.Style; import org.jetbrains.annotations.NotNull; -import static dev.dubhe.curtain.utils.TranslationKeys.*; +import static dev.dubhe.curtain.utils.TranslationKeys.AS_DEFAULT; +import static dev.dubhe.curtain.utils.TranslationKeys.CHANGE; +import static dev.dubhe.curtain.utils.TranslationKeys.CHANGE_DEFAULT; import static net.minecraft.commands.SharedSuggestionProvider.suggest; public class RuleCommand { diff --git a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerLoggedEventHandler.java b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerLoggedEventHandler.java index 2c4bed8..fdb194c 100644 --- a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerLoggedEventHandler.java +++ b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerLoggedEventHandler.java @@ -8,11 +8,12 @@ public class PlayerLoggedEventHandler { @SubscribeEvent - public void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){ - FAKE_PLAYER_INVENTORY_MENU_MAP.put(event.getEntity(),new FakePlayerInventoryMenu(event.getEntity())); + public void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { + FAKE_PLAYER_INVENTORY_MENU_MAP.put(event.getEntity(), new FakePlayerInventoryMenu(event.getEntity())); } + @SubscribeEvent - public void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){ + public void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event) { FAKE_PLAYER_INVENTORY_MENU_MAP.remove(event.getEntity()); } } diff --git a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerTickEventHandler.java b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerTickEventHandler.java index 5d5eb18..3f4f86e 100644 --- a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerTickEventHandler.java +++ b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/PlayerTickEventHandler.java @@ -10,12 +10,12 @@ public class PlayerTickEventHandler { @SubscribeEvent - public void onTick(TickEvent.PlayerTickEvent playerTickEvent){ - if(CurtainRules.openFakePlayerInventory && + public void onTick(TickEvent.PlayerTickEvent playerTickEvent) { + if (CurtainRules.openFakePlayerInventory && playerTickEvent.player instanceof ServerPlayer serverPlayer && serverPlayer instanceof EntityPlayerMPFake && serverPlayer.isAlive() - ){ + ) { FAKE_PLAYER_INVENTORY_MENU_MAP.get(serverPlayer).tick(); } } diff --git a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/entityInteractHandler.java b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/entityInteractHandler.java index 2d14640..2b17ac2 100644 --- a/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/entityInteractHandler.java +++ b/src/main/java/dev/dubhe/curtain/events/rules/open_fake_player_inventory/entityInteractHandler.java @@ -11,12 +11,12 @@ public class entityInteractHandler { @SubscribeEvent - public void onInteractWithFakePlayer(PlayerInteractEvent.EntityInteract entityInteract){ - if(entityInteract.getTarget() instanceof EntityPlayerMPFake fakeplayer){ + public void onInteractWithFakePlayer(PlayerInteractEvent.EntityInteract entityInteract) { + if (entityInteract.getTarget() instanceof EntityPlayerMPFake fakeplayer) { SimpleMenuProvider provider = null; if (CurtainRules.openFakePlayerEnderChest && entityInteract.getEntity().isShiftKeyDown()) { provider = new SimpleMenuProvider( - (i,inventory,p)-> + (i, inventory, p) -> ChestMenu.threeRows( i, inventory, @@ -24,10 +24,9 @@ public void onInteractWithFakePlayer(PlayerInteractEvent.EntityInteract entityIn ), fakeplayer.getDisplayName() ); - } - else if(CurtainRules.openFakePlayerInventory){ + } else if (CurtainRules.openFakePlayerInventory) { provider = new SimpleMenuProvider( - (i,inventory,p)-> + (i, inventory, p) -> ChestMenu.sixRows( i, inventory, @@ -36,7 +35,7 @@ else if(CurtainRules.openFakePlayerInventory){ fakeplayer.getDisplayName() ); } - if(provider != null) + if (provider != null) entityInteract.getEntity().openMenu(provider); } diff --git a/src/main/java/dev/dubhe/curtain/features/logging/AbstractLogger.java b/src/main/java/dev/dubhe/curtain/features/logging/AbstractLogger.java index d85a4e2..43c14f7 100644 --- a/src/main/java/dev/dubhe/curtain/features/logging/AbstractLogger.java +++ b/src/main/java/dev/dubhe/curtain/features/logging/AbstractLogger.java @@ -3,11 +3,10 @@ import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; -import java.util.function.Supplier; - public abstract class AbstractLogger { private final String name; private final DisplayType type; + public AbstractLogger(String name, DisplayType type) { this.name = name; this.type = type; @@ -17,6 +16,7 @@ public AbstractLogger(String name) { this.name = name; this.type = DisplayType.CHAT; } + public String getName() { return name; } diff --git a/src/main/java/dev/dubhe/curtain/features/logging/LoggerManager.java b/src/main/java/dev/dubhe/curtain/features/logging/LoggerManager.java index 182eafa..52a89ed 100644 --- a/src/main/java/dev/dubhe/curtain/features/logging/LoggerManager.java +++ b/src/main/java/dev/dubhe/curtain/features/logging/LoggerManager.java @@ -11,7 +11,12 @@ import net.minecraft.network.chat.MutableComponent; import net.minecraft.server.level.ServerPlayer; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; public class LoggerManager { private static final Map registeredLogger = new HashMap<>(); diff --git a/src/main/java/dev/dubhe/curtain/features/logging/builtin/MemoryLogger.java b/src/main/java/dev/dubhe/curtain/features/logging/builtin/MemoryLogger.java index c5d0a22..0406513 100644 --- a/src/main/java/dev/dubhe/curtain/features/logging/builtin/MemoryLogger.java +++ b/src/main/java/dev/dubhe/curtain/features/logging/builtin/MemoryLogger.java @@ -19,7 +19,7 @@ public Component display(ServerPlayer player) { long usedMemory = totalMemory - freeMemory; MutableComponent msg = Component.empty(); msg.append(Component.literal("%.1f".formatted(usedMemory / 1024 / 1024f) + " M") - .withStyle(ChatFormatting.GRAY)); + .withStyle(ChatFormatting.GRAY)); msg.append(Component.literal(" / ").withStyle(ChatFormatting.WHITE)); msg.append(Component.literal("%.1f".formatted(totalMemory / 1024 / 1024f) + " M") .withStyle(ChatFormatting.GRAY)); diff --git a/src/main/java/dev/dubhe/curtain/features/logging/helper/ExplosionLogHelper.java b/src/main/java/dev/dubhe/curtain/features/logging/helper/ExplosionLogHelper.java index 97a4c88..531c7cd 100644 --- a/src/main/java/dev/dubhe/curtain/features/logging/helper/ExplosionLogHelper.java +++ b/src/main/java/dev/dubhe/curtain/features/logging/helper/ExplosionLogHelper.java @@ -13,7 +13,6 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.Explosion; import net.minecraft.world.phys.Vec3; - import net.minecraftforge.registries.ForgeRegistries; import java.util.ArrayList; diff --git a/src/main/java/dev/dubhe/curtain/features/player/fakes/IEntity.java b/src/main/java/dev/dubhe/curtain/features/player/fakes/IEntity.java index 73a7151..49143c2 100644 --- a/src/main/java/dev/dubhe/curtain/features/player/fakes/IEntity.java +++ b/src/main/java/dev/dubhe/curtain/features/player/fakes/IEntity.java @@ -12,5 +12,6 @@ public interface IEntity { void setPortalTimer(int amount); int getPublicNetherPortalCooldown(); + void setPublicNetherPortalCooldown(int what); } diff --git a/src/main/java/dev/dubhe/curtain/features/player/fakes/IServerPlayer.java b/src/main/java/dev/dubhe/curtain/features/player/fakes/IServerPlayer.java index 721b13b..73ffa57 100644 --- a/src/main/java/dev/dubhe/curtain/features/player/fakes/IServerPlayer.java +++ b/src/main/java/dev/dubhe/curtain/features/player/fakes/IServerPlayer.java @@ -4,7 +4,10 @@ public interface IServerPlayer { EntityPlayerActionPack getActionPack(); + void invalidateEntityObjectReference(); + boolean isInvalidEntityObject(); + String getLanguage(); } diff --git a/src/main/java/dev/dubhe/curtain/features/player/helpers/EntityPlayerActionPack.java b/src/main/java/dev/dubhe/curtain/features/player/helpers/EntityPlayerActionPack.java index 97d07fd..151fda2 100644 --- a/src/main/java/dev/dubhe/curtain/features/player/helpers/EntityPlayerActionPack.java +++ b/src/main/java/dev/dubhe/curtain/features/player/helpers/EntityPlayerActionPack.java @@ -21,7 +21,11 @@ import net.minecraft.world.entity.vehicle.Minecart; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.*; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.EntityHitResult; +import net.minecraft.world.phys.HitResult; +import net.minecraft.world.phys.Vec2; +import net.minecraft.world.phys.Vec3; import java.util.HashMap; import java.util.List; @@ -45,13 +49,12 @@ public class EntityPlayerActionPack { private int itemUseCooldown; - public EntityPlayerActionPack(ServerPlayer playerIn) - { + public EntityPlayerActionPack(ServerPlayer playerIn) { player = playerIn; stopAll(); } - public void copyFrom(EntityPlayerActionPack other) - { + + public void copyFrom(EntityPlayerActionPack other) { actions.putAll(other.actions); currentBlock = other.currentBlock; blockHitDelay = other.blockHitDelay; @@ -66,28 +69,25 @@ public void copyFrom(EntityPlayerActionPack other) itemUseCooldown = other.itemUseCooldown; } - public EntityPlayerActionPack start(ActionType type, Action action) - { + public EntityPlayerActionPack start(ActionType type, Action action) { Action previous = actions.remove(type); if (previous != null) type.stop(player, previous); - if (action != null) - { + if (action != null) { actions.put(type, action); type.start(player, action); // noop } return this; } - public EntityPlayerActionPack setSneaking(boolean doSneak) - { + public EntityPlayerActionPack setSneaking(boolean doSneak) { sneaking = doSneak; player.setShiftKeyDown(doSneak); if (sprinting && sneaking) setSprinting(false); return this; } - public EntityPlayerActionPack setSprinting(boolean doSprint) - { + + public EntityPlayerActionPack setSprinting(boolean doSprint) { sprinting = doSprint; player.setSprinting(doSprint); if (sneaking && sprinting) @@ -95,60 +95,59 @@ public EntityPlayerActionPack setSprinting(boolean doSprint) return this; } - public EntityPlayerActionPack setForward(float value) - { + public EntityPlayerActionPack setForward(float value) { forward = value; return this; } - public EntityPlayerActionPack setStrafing(float value) - { + + public EntityPlayerActionPack setStrafing(float value) { strafing = value; return this; } - public EntityPlayerActionPack look(Direction direction) - { - switch (direction) - { - case NORTH: return look(180, 0); - case SOUTH: return look(0, 0); - case EAST: return look(-90, 0); - case WEST: return look(90, 0); - case UP: return look(player.getYRot(), -90); - case DOWN: return look(player.getYRot(), 90); + + public EntityPlayerActionPack look(Direction direction) { + switch (direction) { + case NORTH: + return look(180, 0); + case SOUTH: + return look(0, 0); + case EAST: + return look(-90, 0); + case WEST: + return look(90, 0); + case UP: + return look(player.getYRot(), -90); + case DOWN: + return look(player.getYRot(), 90); } return this; } - public EntityPlayerActionPack look(Vec2 rotation) - { + + public EntityPlayerActionPack look(Vec2 rotation) { return look(rotation.x, rotation.y); } - public EntityPlayerActionPack look(float yaw, float pitch) - { + public EntityPlayerActionPack look(float yaw, float pitch) { player.setYRot(yaw % 360); //setYaw player.setXRot(Mth.clamp(pitch, -90, 90)); // setPitch // maybe player.setPositionAndAngles(player.x, player.y, player.z, yaw, MathHelper.clamp(pitch,-90.0F, 90.0F)); return this; } - public EntityPlayerActionPack lookAt(Vec3 position) - { + public EntityPlayerActionPack lookAt(Vec3 position) { player.lookAt(EntityAnchorArgument.Anchor.EYES, position); return this; } - public EntityPlayerActionPack turn(float yaw, float pitch) - { + public EntityPlayerActionPack turn(float yaw, float pitch) { return look(player.getYRot() + yaw, player.getXRot() + pitch); } - public EntityPlayerActionPack turn(Vec2 rotation) - { + public EntityPlayerActionPack turn(Vec2 rotation) { return turn(rotation.x, rotation.y); } - public EntityPlayerActionPack stopMovement() - { + public EntityPlayerActionPack stopMovement() { setSneaking(false); setSprinting(false); forward = 0.0F; @@ -157,38 +156,31 @@ public EntityPlayerActionPack stopMovement() } - public EntityPlayerActionPack stopAll() - { + public EntityPlayerActionPack stopAll() { for (ActionType type : actions.keySet()) type.stop(player, actions.get(type)); actions.clear(); return stopMovement(); } - public EntityPlayerActionPack mount(boolean onlyRideables) - { + public EntityPlayerActionPack mount(boolean onlyRideables) { //test what happens List entities; - if (onlyRideables) - { + if (onlyRideables) { entities = player.level.getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D), e -> e instanceof Minecart || e instanceof Boat || e instanceof AbstractHorse); - } - else - { + } else { entities = player.level.getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D)); } - if (entities.size()==0) + if (entities.size() == 0) return this; Entity closest = null; double distance = Double.POSITIVE_INFINITY; Entity currentVehicle = player.getVehicle(); - for (Entity e: entities) - { + for (Entity e : entities) { if (e == player || (currentVehicle == e)) continue; double dd = player.distanceToSqr(e); - if (dd actionAttempts = new HashMap<>(); actions.entrySet().removeIf((e) -> e.getValue().done); - for (Map.Entry e : actions.entrySet()) - { + for (Map.Entry e : actions.entrySet()) { Action action = e.getValue(); // skipping attack if use was successful - if (!(actionAttempts.getOrDefault(ActionType.USE, false) && e.getKey() == ActionType.ATTACK)) - { + if (!(actionAttempts.getOrDefault(ActionType.USE, false) && e.getKey() == ActionType.ATTACK)) { Boolean actionStatus = action.tick(this, e.getKey()); if (actionStatus != null) actionAttempts.put(e.getKey(), actionStatus); } // optionally retrying use after successful attack and unsuccessful use - if ( e.getKey() == ActionType.ATTACK + if (e.getKey() == ActionType.ATTACK && actionAttempts.getOrDefault(ActionType.ATTACK, false) - && !actionAttempts.getOrDefault(ActionType.USE, true) ) - { + && !actionAttempts.getOrDefault(ActionType.USE, true)) { // according to MinecraftClient.handleInputEvents Action using = actions.get(ActionType.USE); if (using != null) // this is always true - we know use worked, but just in case @@ -233,7 +221,7 @@ public void onUpdate() } } } - float vel = sneaking?0.3F:1.0F; + float vel = sneaking ? 0.3F : 1.0F; // The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately if (forward != 0.0F || player instanceof EntityPlayerMPFake) { player.zza = forward * vel; @@ -243,14 +231,12 @@ public void onUpdate() } } - static HitResult getTarget(ServerPlayer player) - { + static HitResult getTarget(ServerPlayer player) { double reach = player.gameMode.isCreative() ? 5 : 4.5f; return Tracer.rayTrace(player, 1, reach, false); } - private void dropItemFromSlot(int slot, boolean dropAll) - { + private void dropItemFromSlot(int slot, boolean dropAll) { Inventory inv = player.getInventory(); // getInventory; if (!inv.getItem(slot).isEmpty()) player.drop(inv.removeItem(slot, @@ -258,15 +244,13 @@ private void dropItemFromSlot(int slot, boolean dropAll) ), false, true); // scatter, keep owner } - public void drop(int selectedSlot, boolean dropAll) - { + public void drop(int selectedSlot, boolean dropAll) { Inventory inv = player.getInventory(); // getInventory; if (selectedSlot == -2) // all { for (int i = inv.getContainerSize(); i >= 0; i--) dropItemFromSlot(i, dropAll); - } - else // one slot + } else // one slot { if (selectedSlot == -1) selectedSlot = inv.selected; @@ -274,93 +258,77 @@ public void drop(int selectedSlot, boolean dropAll) } } - public void setSlot(int slot) - { - player.getInventory().selected = slot-1; - player.connection.send(new ClientboundSetCarriedItemPacket(slot-1)); + public void setSlot(int slot) { + player.getInventory().selected = slot - 1; + player.connection.send(new ClientboundSetCarriedItemPacket(slot - 1)); } - public enum ActionType - { - USE(true) - { - @Override - boolean execute(ServerPlayer player, Action action) - { - EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); - if (ap.itemUseCooldown > 0) - { - ap.itemUseCooldown--; - return true; - } - if (player.isUsingItem()) - { - return true; - } - HitResult hit = getTarget(player); - for (InteractionHand hand : InteractionHand.values()) - { - switch (hit.getType()) - { - case BLOCK: - { - player.resetLastActionTime(); - ServerLevel world = player.getLevel(); - BlockHitResult blockHit = (BlockHitResult) hit; - BlockPos pos = blockHit.getBlockPos(); - Direction side = blockHit.getDirection(); - if (pos.getY() < player.getLevel().getMaxBuildHeight() - (side == Direction.UP ? 1 : 0) && world.mayInteract(player, pos)) - { - InteractionResult result = player.gameMode.useItemOn(player, world, player.getItemInHand(hand), hand, blockHit); - if (result.consumesAction()) - { - if (result.shouldSwing()) player.swing(hand); - ap.itemUseCooldown = 3; - return true; - } - } - break; - } - case ENTITY: - { - player.resetLastActionTime(); - EntityHitResult entityHit = (EntityHitResult) hit; - Entity entity = entityHit.getEntity(); - boolean handWasEmpty = player.getItemInHand(hand).isEmpty(); - boolean itemFrameEmpty = (entity instanceof ItemFrame) && ((ItemFrame) entity).getItem().isEmpty(); - Vec3 relativeHitPos = entityHit.getLocation().subtract(entity.getX(), entity.getY(), entity.getZ()); - if (entity.interactAt(player, relativeHitPos, hand).consumesAction()) - { - ap.itemUseCooldown = 3; - return true; - } - // fix for SS itemframe always returns CONSUME even if no action is performed - if (player.interactOn(entity, hand).consumesAction() && !(handWasEmpty && itemFrameEmpty)) - { - ap.itemUseCooldown = 3; - return true; - } - break; + public enum ActionType { + USE(true) { + @Override + boolean execute(ServerPlayer player, Action action) { + EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); + if (ap.itemUseCooldown > 0) { + ap.itemUseCooldown--; + return true; + } + if (player.isUsingItem()) { + return true; + } + HitResult hit = getTarget(player); + for (InteractionHand hand : InteractionHand.values()) { + switch (hit.getType()) { + case BLOCK: { + player.resetLastActionTime(); + ServerLevel world = player.getLevel(); + BlockHitResult blockHit = (BlockHitResult) hit; + BlockPos pos = blockHit.getBlockPos(); + Direction side = blockHit.getDirection(); + if (pos.getY() < player.getLevel().getMaxBuildHeight() - (side == Direction.UP ? 1 : 0) && world.mayInteract(player, pos)) { + InteractionResult result = player.gameMode.useItemOn(player, world, player.getItemInHand(hand), hand, blockHit); + if (result.consumesAction()) { + if (result.shouldSwing()) player.swing(hand); + ap.itemUseCooldown = 3; + return true; } } - ItemStack handItem = player.getItemInHand(hand); - if (player.gameMode.useItem(player, player.getLevel(), handItem, hand).consumesAction()) - { + break; + } + case ENTITY: { + player.resetLastActionTime(); + EntityHitResult entityHit = (EntityHitResult) hit; + Entity entity = entityHit.getEntity(); + boolean handWasEmpty = player.getItemInHand(hand).isEmpty(); + boolean itemFrameEmpty = (entity instanceof ItemFrame) && ((ItemFrame) entity).getItem().isEmpty(); + Vec3 relativeHitPos = entityHit.getLocation().subtract(entity.getX(), entity.getY(), entity.getZ()); + if (entity.interactAt(player, relativeHitPos, hand).consumesAction()) { ap.itemUseCooldown = 3; return true; } + // fix for SS itemframe always returns CONSUME even if no action is performed + if (player.interactOn(entity, hand).consumesAction() && !(handWasEmpty && itemFrameEmpty)) { + ap.itemUseCooldown = 3; + return true; + } + break; } - return false; } - - @Override - void inactiveTick(ServerPlayer player, Action action) - { - EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); - ap.itemUseCooldown = 0; - player.releaseUsingItem(); + ItemStack handItem = player.getItemInHand(hand); + if (player.gameMode.useItem(player, player.getLevel(), handItem, hand).consumesAction()) { + ap.itemUseCooldown = 3; + return true; } - }, + } + return false; + } + + @Override + void inactiveTick(ServerPlayer player, Action action) { + EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); + ap.itemUseCooldown = 0; + player.releaseUsingItem(); + } + }, ATTACK(true) { @Override boolean execute(ServerPlayer player, Action action) { @@ -368,8 +336,7 @@ boolean execute(ServerPlayer player, Action action) { switch (hit.getType()) { case ENTITY: { EntityHitResult entityHit = (EntityHitResult) hit; - if (!action.isContinuous) - { + if (!action.isContinuous) { player.attack(entityHit.getEntity()); player.swing(InteractionHand.MAIN_HAND); } @@ -379,59 +346,47 @@ boolean execute(ServerPlayer player, Action action) { } case BLOCK: { EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); - if(!action.isContinuous && player.gameMode.isCreative()) + if (!action.isContinuous && player.gameMode.isCreative()) ap.blockHitDelay = 0; //Reset delay when attack(once) - if (ap.blockHitDelay > 0) - { + if (ap.blockHitDelay > 0) { ap.blockHitDelay--; return false; } BlockHitResult blockHit = (BlockHitResult) hit; BlockPos pos = blockHit.getBlockPos(); Direction side = blockHit.getDirection(); - if (player.blockActionRestricted(player.level, pos, player.gameMode.getGameModeForPlayer())) return false; - if (ap.currentBlock != null && player.level.getBlockState(ap.currentBlock).isAir()) - { + if (player.blockActionRestricted(player.level, pos, player.gameMode.getGameModeForPlayer())) + return false; + if (ap.currentBlock != null && player.level.getBlockState(ap.currentBlock).isAir()) { ap.currentBlock = null; return false; } BlockState state = player.level.getBlockState(pos); boolean blockBroken = false; - if (player.gameMode.getGameModeForPlayer().isCreative()) - { + if (player.gameMode.getGameModeForPlayer().isCreative()) { player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.getLevel().getMaxBuildHeight(), -1); ap.blockHitDelay = 5; blockBroken = true; - } - else if (ap.currentBlock == null || !ap.currentBlock.equals(pos)) - { - if (ap.currentBlock != null) - { + } else if (ap.currentBlock == null || !ap.currentBlock.equals(pos)) { + if (ap.currentBlock != null) { player.gameMode.handleBlockBreakAction(ap.currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, side, player.getLevel().getMaxBuildHeight(), -1); } player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.getLevel().getMaxBuildHeight(), -1); boolean notAir = !state.isAir(); - if (notAir && ap.curBlockDamageMP == 0) - { + if (notAir && ap.curBlockDamageMP == 0) { state.attack(player.level, pos, player); } - if (notAir && state.getDestroyProgress(player, player.level, pos) >= 1) - { + if (notAir && state.getDestroyProgress(player, player.level, pos) >= 1) { ap.currentBlock = null; //instamine?? blockBroken = true; - } - else - { + } else { ap.currentBlock = pos; ap.curBlockDamageMP = 0; } - } - else - { + } else { ap.curBlockDamageMP += state.getDestroyProgress(player, player.level, pos); - if (ap.curBlockDamageMP >= 1) - { + if (ap.curBlockDamageMP >= 1) { player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, side, player.getLevel().getMaxBuildHeight(), -1); ap.currentBlock = null; ap.blockHitDelay = 5; @@ -449,8 +404,7 @@ else if (ap.currentBlock == null || !ap.currentBlock.equals(pos)) } @Override - void inactiveTick(ServerPlayer player, Action action) - { + void inactiveTick(ServerPlayer player, Action action) { EntityPlayerActionPack ap = ((IServerPlayer) player).getActionPack(); if (ap.currentBlock == null) return; player.level.destroyBlockProgress(-1, ap.currentBlock, -1); @@ -458,79 +412,69 @@ void inactiveTick(ServerPlayer player, Action action) ap.currentBlock = null; } }, - JUMP(true) - { - @Override - boolean execute(ServerPlayer player, Action action) - { - if (action.limit == 1) - { - if (player.isOnGround()) player.jumpFromGround(); // onGround - } - else - { - player.setJumping(true); - } - return false; - } + JUMP(true) { + @Override + boolean execute(ServerPlayer player, Action action) { + if (action.limit == 1) { + if (player.isOnGround()) player.jumpFromGround(); // onGround + } else { + player.setJumping(true); + } + return false; + } - @Override - void inactiveTick(ServerPlayer player, Action action) - { - player.setJumping(false); - } - }, - DROP_ITEM(true) - { - @Override - boolean execute(ServerPlayer player, Action action) - { - player.resetLastActionTime(); - player.drop(false); // dropSelectedItem - return false; - } - }, - DROP_STACK(true) - { - @Override - boolean execute(ServerPlayer player, Action action) - { - player.resetLastActionTime(); - player.drop(true); // dropSelectedItem - return false; - } - }, - SWAP_HANDS(true) - { - @Override - boolean execute(ServerPlayer player, Action action) - { - player.resetLastActionTime(); - ItemStack itemStack_1 = player.getItemInHand(InteractionHand.OFF_HAND); - player.setItemInHand(InteractionHand.OFF_HAND, player.getItemInHand(InteractionHand.MAIN_HAND)); - player.setItemInHand(InteractionHand.MAIN_HAND, itemStack_1); - return false; - } - }; + @Override + void inactiveTick(ServerPlayer player, Action action) { + player.setJumping(false); + } + }, + DROP_ITEM(true) { + @Override + boolean execute(ServerPlayer player, Action action) { + player.resetLastActionTime(); + player.drop(false); // dropSelectedItem + return false; + } + }, + DROP_STACK(true) { + @Override + boolean execute(ServerPlayer player, Action action) { + player.resetLastActionTime(); + player.drop(true); // dropSelectedItem + return false; + } + }, + SWAP_HANDS(true) { + @Override + boolean execute(ServerPlayer player, Action action) { + player.resetLastActionTime(); + ItemStack itemStack_1 = player.getItemInHand(InteractionHand.OFF_HAND); + player.setItemInHand(InteractionHand.OFF_HAND, player.getItemInHand(InteractionHand.MAIN_HAND)); + player.setItemInHand(InteractionHand.MAIN_HAND, itemStack_1); + return false; + } + }; public final boolean preventSpectator; - ActionType(boolean preventSpectator) - { + ActionType(boolean preventSpectator) { this.preventSpectator = preventSpectator; } - void start(ServerPlayer player, Action action) {} + void start(ServerPlayer player, Action action) { + } + abstract boolean execute(ServerPlayer player, Action action); - void inactiveTick(ServerPlayer player, Action action) {} - void stop(ServerPlayer player, Action action) - { + + void inactiveTick(ServerPlayer player, Action action) { + } + + void stop(ServerPlayer player, Action action) { inactiveTick(player, action); } } - public static class Action - { + public static class Action { public boolean done = false; public final int limit; public final int interval; @@ -539,8 +483,7 @@ public static class Action private int next; private final boolean isContinuous; - private Action(int limit, int interval, int offset, boolean continuous) - { + private Action(int limit, int interval, int offset, boolean continuous) { this.limit = limit; this.interval = interval; this.offset = offset; @@ -548,75 +491,59 @@ private Action(int limit, int interval, int offset, boolean continuous) isContinuous = continuous; } - public static Action once() - { + public static Action once() { return new Action(1, 1, 0, false); } - public static Action continuous() - { + public static Action continuous() { return new Action(-1, 1, 0, true); } - public static Action interval(int interval) - { + public static Action interval(int interval) { return new Action(-1, interval, 0, false); } - public static Action interval(int interval, int offset) - { + public static Action interval(int interval, int offset) { return new Action(-1, interval, offset, false); } - Boolean tick(EntityPlayerActionPack actionPack, ActionType type) - { + Boolean tick(EntityPlayerActionPack actionPack, ActionType type) { next--; Boolean cancel = null; - if (next <= 0) - { - if (interval == 1 && !isContinuous) - { + if (next <= 0) { + if (interval == 1 && !isContinuous) { // need to allow entity to tick, otherwise won't have effect (bow) // actions are 20 tps, so need to clear status mid tick, allowing entities process it till next time - if (!type.preventSpectator || !actionPack.player.isSpectator()) - { + if (!type.preventSpectator || !actionPack.player.isSpectator()) { type.inactiveTick(actionPack.player, this); } } - if (!type.preventSpectator || !actionPack.player.isSpectator()) - { + if (!type.preventSpectator || !actionPack.player.isSpectator()) { cancel = type.execute(actionPack.player, this); } count++; - if (count == limit) - { + if (count == limit) { type.stop(actionPack.player, null); done = true; return cancel; } next = interval; - } - else - { - if (!type.preventSpectator || !actionPack.player.isSpectator()) - { + } else { + if (!type.preventSpectator || !actionPack.player.isSpectator()) { type.inactiveTick(actionPack.player, this); } } return cancel; } - void retry(EntityPlayerActionPack actionPack, ActionType type) - { + void retry(EntityPlayerActionPack actionPack, ActionType type) { //assuming action run but was unsuccesful that tick, but opportunity emerged to retry it, lets retry it. - if (!type.preventSpectator || !actionPack.player.isSpectator()) - { + if (!type.preventSpectator || !actionPack.player.isSpectator()) { type.execute(actionPack.player, this); } count++; - if (count == limit) - { + if (count == limit) { type.stop(actionPack.player, null); done = true; } diff --git a/src/main/java/dev/dubhe/curtain/features/player/menu/FakePlayerInventoryMenu.java b/src/main/java/dev/dubhe/curtain/features/player/menu/FakePlayerInventoryMenu.java index b34e823..c8f22dd 100644 --- a/src/main/java/dev/dubhe/curtain/features/player/menu/FakePlayerInventoryMenu.java +++ b/src/main/java/dev/dubhe/curtain/features/player/menu/FakePlayerInventoryMenu.java @@ -29,48 +29,48 @@ public class FakePlayerInventoryMenu implements Container { public final NonNullList Armor; public final NonNullList Offhand; - private final NonNullList Buttons = NonNullList.withSize(13,ItemStack.EMPTY); + private final NonNullList Buttons = NonNullList.withSize(13, ItemStack.EMPTY); private final List