From 7f2cb0f4a70b1a502327203c7b3ff94f5c79ab14 Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 24 May 2021 13:03:28 +0200 Subject: [PATCH 1/4] Implement `@Locator.Block` --- .../carpet/script/annotation/Locator.java | 11 +++----- .../carpet/script/argument/BlockArgument.java | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/carpet/script/annotation/Locator.java b/src/main/java/carpet/script/annotation/Locator.java index f4d49345..f5fe1840 100644 --- a/src/main/java/carpet/script/annotation/Locator.java +++ b/src/main/java/carpet/script/annotation/Locator.java @@ -8,6 +8,7 @@ import java.util.Iterator; import org.apache.commons.lang3.NotImplementedException; +import carpet.script.CarpetContext; import carpet.script.Context; import carpet.script.argument.Argument; import carpet.script.argument.BlockArgument; @@ -25,8 +26,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; /** *

Class that holds annotation for {@link Argument} locators to be used in Scarpet functions.

* - *

Note: Nothing in this class has been implemented at this point in time, since it requires Carpet changes. There is an implementation for - * Locator.Block working, but not implemented.

+ *

Note: Only {@link Block} locator is currently implemented.

*/ public interface Locator { @@ -38,7 +38,6 @@ public interface Locator @Documented @Retention(RUNTIME) @Target({ PARAMETER, TYPE_USE }) - @Deprecated // Not implemented, although implementation available public @interface Block { /** @@ -166,10 +165,8 @@ public interface Locator @Override public R checkAndConvert(Iterator valueIterator, Context context, Context.Type theLazyT) { - BlockArgument locator = null; // BlockArgument.findIn((CarpetContext)context, valueIterator (requires changing to Value), 0, - // acceptString, optional, anyString); - // return (R) (returnBlockValue ? locator.block : locator); - throw new NotImplementedException("Locator.Block still requires adapting BlockArgument to accept iterators (which is actually simple)"); + BlockArgument locator = BlockArgument.findIn((CarpetContext) context, valueIterator, 0, acceptString, optional, anyString); + return (R) (returnBlockValue ? locator.block : locator); } } diff --git a/src/main/java/carpet/script/argument/BlockArgument.java b/src/main/java/carpet/script/argument/BlockArgument.java index 97f3ffd2..86468d06 100644 --- a/src/main/java/carpet/script/argument/BlockArgument.java +++ b/src/main/java/carpet/script/argument/BlockArgument.java @@ -1,7 +1,6 @@ package carpet.script.argument; import carpet.script.CarpetContext; -import carpet.script.LazyValue; import carpet.script.exception.InternalExpressionException; import carpet.script.value.BlockValue; import carpet.script.value.ListValue; @@ -11,7 +10,9 @@ import carpet.script.value.StringValue; import carpet.script.value.Value; import net.minecraft.util.math.BlockPos; +import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; public class BlockArgument extends Argument { @@ -41,26 +42,31 @@ public class BlockArgument extends Argument } public static BlockArgument findIn(CarpetContext c, List params, int offset, boolean acceptString, boolean optional, boolean anyString) + { + return findIn(c, params.listIterator(offset), offset, acceptString, optional, anyString); + } + + public static BlockArgument findIn(CarpetContext c, Iterator params, int offset, boolean acceptString, boolean optional, boolean anyString) { try { - Value v1 = params.get(0 + offset); + Value v1 = params.next(); //add conditional from string name if (optional && v1 instanceof NullValue) { - return new BlockArgument(null, 1+offset); + return new BlockArgument(null, 1 + offset); } if (anyString && v1 instanceof StringValue) { - return new BlockArgument(null, 1+offset, v1.getString()); + return new BlockArgument(null, 1 + offset, v1.getString()); } if (acceptString && v1 instanceof StringValue) { - return new BlockArgument(BlockValue.fromString(v1.getString()), 1+offset); + return new BlockArgument(BlockValue.fromString(v1.getString()), 1 + offset); } if (v1 instanceof BlockValue) { - return new BlockArgument(((BlockValue) v1), 1+offset); + return new BlockArgument(((BlockValue) v1), 1 + offset); } if (v1 instanceof ListValue) { @@ -74,21 +80,21 @@ public class BlockArgument extends Argument c.s.getWorld(), new BlockPos(c.origin.getX() + xpos, c.origin.getY() + ypos, c.origin.getZ() + zpos) ), - 1+offset); + 1 + offset); } int xpos = (int) NumericValue.asNumber(v1).getLong(); - int ypos = (int) NumericValue.asNumber( params.get(1 + offset)).getLong(); - int zpos = (int) NumericValue.asNumber( params.get(2 + offset)).getLong(); + int ypos = (int) NumericValue.asNumber( params.next()).getLong(); + int zpos = (int) NumericValue.asNumber( params.next()).getLong(); return new BlockArgument( new BlockValue( null, c.s.getWorld(), new BlockPos(c.origin.getX() + xpos, c.origin.getY() + ypos, c.origin.getZ() + zpos) ), - 3+offset + 3 + offset ); } - catch (IndexOutOfBoundsException e) + catch (IndexOutOfBoundsException | NoSuchElementException e) { throw handleError(optional, acceptString); } From ca1828d797e5854ada64ac3a81d420d97ad2c91a Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 24 May 2021 14:14:03 +0200 Subject: [PATCH 2/4] Allow more parameter types in `@Locator.Block` --- .../carpet/script/annotation/Locator.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/carpet/script/annotation/Locator.java b/src/main/java/carpet/script/annotation/Locator.java index f5fe1840..bbaafa94 100644 --- a/src/main/java/carpet/script/annotation/Locator.java +++ b/src/main/java/carpet/script/annotation/Locator.java @@ -18,6 +18,8 @@ import carpet.script.bundled.Module; import carpet.script.value.BlockValue; import carpet.script.value.FunctionValue; import carpet.script.value.Value; +import net.minecraft.block.BlockState; +import net.minecraft.util.math.BlockPos; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.TYPE_USE; @@ -33,7 +35,7 @@ public interface Locator /** *

Represents that the annotated argument must be gotten by passing the arguments in there into a {@link BlockArgument} locator.

* - *

Must be used in either {@link BlockValue} or {@link BlockArgument} parameters

+ *

Must be used in either {@link BlockArgument}, {@link BlockValue} or {@link BlockPos} parameters

*/ @Documented @Retention(RUNTIME) @@ -139,7 +141,7 @@ public interface Locator private static class BlockLocator extends AbstractLocator { - private final boolean returnBlockValue; + private final java.util.function.Function returnFunction; private final boolean acceptString; private final boolean anyString; private final boolean optional; @@ -149,11 +151,24 @@ public interface Locator this.acceptString = annotation.acceptString(); this.anyString = annotation.anyString(); this.optional = annotation.optional(); - this.returnBlockValue = type == BlockValue.class; - if (returnBlockValue && (anyString || optional)) + if (type != BlockArgument.class && (anyString || optional)) throw new IllegalArgumentException("Can only use anyString or optional parameters of Locator.Block if targeting a BlockArgument"); - if (!returnBlockValue && type != BlockArgument.class) - throw new IllegalArgumentException("Locator.Block can only be used against BlockArgument or BlockValue types!"); + this.returnFunction = getReturnFunction(type); + if (returnFunction == null) + throw new IllegalArgumentException("Locator.Block can only be used against BlockArgument, BlockValue, BlockPos or BlockState types!"); + } + + @SuppressWarnings("unchecked") + private static java.util.function.Function getReturnFunction(Class type) { + if (type == BlockArgument.class) + return r -> (R) r; + if (type == BlockValue.class) + return r -> (R) r.block; + if (type == BlockPos.class) + return r -> (R) r.block.getPos(); + if (type == BlockState.class) + return r -> (R) r.block.getBlockState(); + return null; } @Override @@ -166,7 +181,7 @@ public interface Locator public R checkAndConvert(Iterator valueIterator, Context context, Context.Type theLazyT) { BlockArgument locator = BlockArgument.findIn((CarpetContext) context, valueIterator, 0, acceptString, optional, anyString); - return (R) (returnBlockValue ? locator.block : locator); + return returnFunction.apply(locator); } } From aeba4520d4c03a6c3913e58a3ab454a20066574d Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 24 May 2021 14:18:33 +0200 Subject: [PATCH 3/4] Fix missing type in javadoc of `@Locator.Block` --- src/main/java/carpet/script/annotation/Locator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/carpet/script/annotation/Locator.java b/src/main/java/carpet/script/annotation/Locator.java index bbaafa94..1f0e87e1 100644 --- a/src/main/java/carpet/script/annotation/Locator.java +++ b/src/main/java/carpet/script/annotation/Locator.java @@ -35,7 +35,7 @@ public interface Locator /** *

Represents that the annotated argument must be gotten by passing the arguments in there into a {@link BlockArgument} locator.

* - *

Must be used in either {@link BlockArgument}, {@link BlockValue} or {@link BlockPos} parameters

+ *

Must be used in either {@link BlockArgument}, {@link BlockValue}, {@link BlockPos} or {@link BlockState} parameters

*/ @Documented @Retention(RUNTIME) From 0cb20e07957d9d7ac9ceef62e1b3bac455bdc174 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 26 May 2021 00:21:59 +0800 Subject: [PATCH 4/4] Allow `/player spawn` spawn offline-mode player in online server; Code clean-up in `/player spawn` command (#872) * Clean up code in PlayerCommand.spawn and EntityPlayerMPFake.createFake. * Update src/main/java/carpet/CarpetSettings.java * Update src/main/java/carpet/commands/PlayerCommand.java * Update src/main/java/carpet/patches/EntityPlayerMPFake.java Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com> --- src/main/java/carpet/CarpetSettings.java | 3 +++ src/main/java/carpet/commands/PlayerCommand.java | 11 ++++++++--- src/main/java/carpet/patches/EntityPlayerMPFake.java | 12 ++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/carpet/CarpetSettings.java b/src/main/java/carpet/CarpetSettings.java index eb7723e8..1d04c622 100644 --- a/src/main/java/carpet/CarpetSettings.java +++ b/src/main/java/carpet/CarpetSettings.java @@ -472,6 +472,9 @@ public class CarpetSettings @Rule(desc = "Enables /player command to control/spawn players", category = COMMAND) public static String commandPlayer = "ops"; + @Rule(desc = "Spawn offline players in online mode if online-mode player with specified name does not exist", category = COMMAND) + public static boolean allowSpawningOfflinePlayers = true; + @Rule(desc = "Allows to track mobs AI via /track command", category = COMMAND) public static String commandTrackAI = "ops"; diff --git a/src/main/java/carpet/commands/PlayerCommand.java b/src/main/java/carpet/commands/PlayerCommand.java index ee52afe4..eacfe1bd 100644 --- a/src/main/java/carpet/commands/PlayerCommand.java +++ b/src/main/java/carpet/commands/PlayerCommand.java @@ -192,9 +192,14 @@ public class PlayerCommand GameProfile profile = server.getUserCache().findByName(playerName); if (profile == null) { - Messenger.m(context.getSource(), "r Player "+playerName+" is either banned by Mojang, or auth servers are down. " + - "Banned players can only be summoned in Singleplayer and in servers in off-line mode."); - return true; + if (!CarpetSettings.allowSpawningOfflinePlayers) + { + Messenger.m(context.getSource(), "r Player "+playerName+" is either banned by Mojang, or auth servers are down. " + + "Banned players can only be summoned in Singleplayer and in servers in off-line mode."); + return true; + } else { + profile = new GameProfile(PlayerEntity.getOfflinePlayerUuid(playerName), playerName); + } } if (manager.getUserBanList().contains(profile)) { diff --git a/src/main/java/carpet/patches/EntityPlayerMPFake.java b/src/main/java/carpet/patches/EntityPlayerMPFake.java index b0cf86f3..8d6ca7d2 100644 --- a/src/main/java/carpet/patches/EntityPlayerMPFake.java +++ b/src/main/java/carpet/patches/EntityPlayerMPFake.java @@ -1,5 +1,6 @@ package carpet.patches; +import carpet.CarpetSettings; import com.mojang.authlib.GameProfile; import net.minecraft.block.entity.SkullBlockEntity; import net.minecraft.entity.Entity; @@ -22,7 +23,6 @@ import net.minecraft.util.UserCache; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.GameMode; import net.minecraft.world.World; -import net.minecraft.world.dimension.DimensionType; import carpet.fakes.ServerPlayerEntityInterface; import carpet.utils.Messenger; @@ -45,7 +45,15 @@ public class EntityPlayerMPFake extends ServerPlayerEntity finally { UserCache.setUseRemote(server.isDedicated() && server.isOnlineMode()); } - if (gameprofile == null) return null; + if (gameprofile == null) + { + if (!CarpetSettings.allowSpawningOfflinePlayers) + { + return null; + } else { + gameprofile = new GameProfile(PlayerEntity.getOfflinePlayerUuid(username), username); + } + } if (gameprofile.getProperties().containsKey("textures")) { gameprofile = SkullBlockEntity.loadProperties(gameprofile);