This commit is contained in:
gnembon committed 2021-05-25 17:24:29 -03:00
commit eb927f5e7c
5 files changed
+63 -29

No files matched your search

+3
View File
@@ -470,6 +470,9 @@ public class CarpetSettings
@Rule(desc = "Enables /player command to control/spawn players", category = COMMAND) @Rule(desc = "Enables /player command to control/spawn players", category = COMMAND)
public static String commandPlayer = "ops"; 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) @Rule(desc = "Allows to track mobs AI via /track command", category = COMMAND)
public static String commandTrackAI = "ops"; public static String commandTrackAI = "ops";
@@ -192,9 +192,14 @@ public class PlayerCommand
GameProfile profile = server.getUserCache().findByName(playerName); GameProfile profile = server.getUserCache().findByName(playerName);
if (profile == null) if (profile == null)
{ {
Messenger.m(context.getSource(), "r Player "+playerName+" is either banned by Mojang, or auth servers are down. " + if (!CarpetSettings.allowSpawningOfflinePlayers)
"Banned players can only be summoned in Singleplayer and in servers in off-line mode."); {
return true; 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)) if (manager.getUserBanList().contains(profile))
{ {
@@ -1,5 +1,6 @@
package carpet.patches; package carpet.patches;
import carpet.CarpetSettings;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.block.entity.SkullBlockEntity; import net.minecraft.block.entity.SkullBlockEntity;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -22,7 +23,6 @@ import net.minecraft.util.UserCache;
import net.minecraft.util.registry.RegistryKey; import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.GameMode; import net.minecraft.world.GameMode;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import carpet.fakes.ServerPlayerEntityInterface; import carpet.fakes.ServerPlayerEntityInterface;
import carpet.utils.Messenger; import carpet.utils.Messenger;
@@ -45,7 +45,15 @@ public class EntityPlayerMPFake extends ServerPlayerEntity
finally { finally {
UserCache.setUseRemote(server.isDedicated() && server.isOnlineMode()); 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")) if (gameprofile.getProperties().containsKey("textures"))
{ {
gameprofile = SkullBlockEntity.loadProperties(gameprofile); gameprofile = SkullBlockEntity.loadProperties(gameprofile);
@@ -8,6 +8,7 @@ import java.util.Iterator;
import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.NotImplementedException;
import carpet.script.CarpetContext;
import carpet.script.Context; import carpet.script.Context;
import carpet.script.argument.Argument; import carpet.script.argument.Argument;
import carpet.script.argument.BlockArgument; import carpet.script.argument.BlockArgument;
@@ -17,6 +18,8 @@ import carpet.script.bundled.Module;
import carpet.script.value.BlockValue; import carpet.script.value.BlockValue;
import carpet.script.value.FunctionValue; import carpet.script.value.FunctionValue;
import carpet.script.value.Value; 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.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.ElementType.TYPE_USE;
@@ -25,20 +28,18 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* <p>Class that holds annotation for {@link Argument} locators to be used in Scarpet functions.</p> * <p>Class that holds annotation for {@link Argument} locators to be used in Scarpet functions.</p>
* *
* <p>Note: Nothing in this class has been implemented at this point in time, since it requires Carpet changes. There is an implementation for * <p>Note: Only {@link Block} locator is currently implemented.</p>
* Locator.Block working, but not implemented.</p>
*/ */
public interface Locator public interface Locator
{ {
/** /**
* <p>Represents that the annotated argument must be gotten by passing the arguments in there into a {@link BlockArgument} locator.</p> * <p>Represents that the annotated argument must be gotten by passing the arguments in there into a {@link BlockArgument} locator.</p>
* *
* <p>Must be used in either {@link BlockValue} or {@link BlockArgument} parameters</p> * <p>Must be used in either {@link BlockArgument}, {@link BlockValue}, {@link BlockPos} or {@link BlockState} parameters</p>
*/ */
@Documented @Documented
@Retention(RUNTIME) @Retention(RUNTIME)
@Target({ PARAMETER, TYPE_USE }) @Target({ PARAMETER, TYPE_USE })
@Deprecated // Not implemented, although implementation available
public @interface Block public @interface Block
{ {
/** /**
@@ -140,7 +141,7 @@ public interface Locator
private static class BlockLocator<R> extends AbstractLocator<R> private static class BlockLocator<R> extends AbstractLocator<R>
{ {
private final boolean returnBlockValue; private final java.util.function.Function<BlockArgument, R> returnFunction;
private final boolean acceptString; private final boolean acceptString;
private final boolean anyString; private final boolean anyString;
private final boolean optional; private final boolean optional;
@@ -150,11 +151,24 @@ public interface Locator
this.acceptString = annotation.acceptString(); this.acceptString = annotation.acceptString();
this.anyString = annotation.anyString(); this.anyString = annotation.anyString();
this.optional = annotation.optional(); this.optional = annotation.optional();
this.returnBlockValue = type == BlockValue.class; if (type != BlockArgument.class && (anyString || optional))
if (returnBlockValue && (anyString || optional))
throw new IllegalArgumentException("Can only use anyString or optional parameters of Locator.Block if targeting a BlockArgument"); throw new IllegalArgumentException("Can only use anyString or optional parameters of Locator.Block if targeting a BlockArgument");
if (!returnBlockValue && type != BlockArgument.class) this.returnFunction = getReturnFunction(type);
throw new IllegalArgumentException("Locator.Block can only be used against BlockArgument or BlockValue types!"); if (returnFunction == null)
throw new IllegalArgumentException("Locator.Block can only be used against BlockArgument, BlockValue, BlockPos or BlockState types!");
}
@SuppressWarnings("unchecked")
private static <R> java.util.function.Function<BlockArgument, R> getReturnFunction(Class<R> 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 @Override
@@ -166,10 +180,8 @@ public interface Locator
@Override @Override
public R checkAndConvert(Iterator<Value> valueIterator, Context context, Context.Type theLazyT) public R checkAndConvert(Iterator<Value> valueIterator, Context context, Context.Type theLazyT)
{ {
BlockArgument locator = null; // BlockArgument.findIn((CarpetContext)context, valueIterator (requires changing to Value), 0, BlockArgument locator = BlockArgument.findIn((CarpetContext) context, valueIterator, 0, acceptString, optional, anyString);
// acceptString, optional, anyString); return returnFunction.apply(locator);
// return (R) (returnBlockValue ? locator.block : locator);
throw new NotImplementedException("Locator.Block still requires adapting BlockArgument to accept iterators (which is actually simple)");
} }
} }
@@ -1,7 +1,6 @@
package carpet.script.argument; package carpet.script.argument;
import carpet.script.CarpetContext; import carpet.script.CarpetContext;
import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.value.BlockValue; import carpet.script.value.BlockValue;
import carpet.script.value.ListValue; import carpet.script.value.ListValue;
@@ -11,7 +10,9 @@ import carpet.script.value.StringValue;
import carpet.script.value.Value; import carpet.script.value.Value;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
public class BlockArgument extends Argument public class BlockArgument extends Argument
{ {
@@ -41,26 +42,31 @@ public class BlockArgument extends Argument
} }
public static BlockArgument findIn(CarpetContext c, List<Value> params, int offset, boolean acceptString, boolean optional, boolean anyString) public static BlockArgument findIn(CarpetContext c, List<Value> 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<Value> params, int offset, boolean acceptString, boolean optional, boolean anyString)
{ {
try try
{ {
Value v1 = params.get(0 + offset); Value v1 = params.next();
//add conditional from string name //add conditional from string name
if (optional && v1 instanceof NullValue) if (optional && v1 instanceof NullValue)
{ {
return new BlockArgument(null, 1+offset); return new BlockArgument(null, 1 + offset);
} }
if (anyString && v1 instanceof StringValue) 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) 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) if (v1 instanceof BlockValue)
{ {
return new BlockArgument(((BlockValue) v1), 1+offset); return new BlockArgument(((BlockValue) v1), 1 + offset);
} }
if (v1 instanceof ListValue) if (v1 instanceof ListValue)
{ {
@@ -74,21 +80,21 @@ public class BlockArgument extends Argument
c.s.getWorld(), c.s.getWorld(),
new BlockPos(c.origin.getX() + xpos, c.origin.getY() + ypos, c.origin.getZ() + zpos) 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 xpos = (int) NumericValue.asNumber(v1).getLong();
int ypos = (int) NumericValue.asNumber( params.get(1 + offset)).getLong(); int ypos = (int) NumericValue.asNumber( params.next()).getLong();
int zpos = (int) NumericValue.asNumber( params.get(2 + offset)).getLong(); int zpos = (int) NumericValue.asNumber( params.next()).getLong();
return new BlockArgument( return new BlockArgument(
new BlockValue( new BlockValue(
null, null,
c.s.getWorld(), c.s.getWorld(),
new BlockPos(c.origin.getX() + xpos, c.origin.getY() + ypos, c.origin.getZ() + zpos) 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); throw handleError(optional, acceptString);
} }