Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2020-11-25 02:12:41 -04:00
commit 253604b34a
18 files changed
+381 -32

No files matched your search

@@ -268,6 +268,12 @@ public class CarpetEventServer
};
public static final Event NETHER_TICK = new Event("tick_nether", 0, true)
{
@Override
public boolean deprecated()
{
return true;
}
@Override
public void onTick()
{
@@ -279,6 +285,11 @@ public class CarpetEventServer
};
public static final Event ENDER_TICK = new Event("tick_ender", 0, true)
{
@Override
public boolean deprecated()
{
return true;
}
@Override
public void onTick()
{
@@ -804,6 +815,7 @@ public class CarpetEventServer
{
return handler.callList.size() > 0;
}
public boolean deprecated() {return false;}
//stubs for calls just to ease calls in vanilla code so they don't need to deal with scarpet value types
public void onTick() { }
public void onChunkGenerated(ServerWorld world, Chunk chunk) { }
@@ -900,10 +912,10 @@ public class CarpetEventServer
private boolean canAddEvent(Event event, ScriptHost host)
{
if (event.deprecated()) host.issueDeprecation(event.name+" event");
return !(event.globalOnly && (host.perUser || host.parent != null));
}
public boolean removeEventFromCommand(ServerCommandSource source, String event, String funName)
{
Event ev = Event.getEvent(event, CarpetServer.scriptServer);
@@ -57,7 +57,10 @@ public class CarpetScriptHost extends ScriptHost
public Map<Value, Value> appConfig;
public Map<String, CommandArgument> appArgTypes;
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes)
Function<ServerCommandSource, Boolean> commandValidator;
boolean isRuleApp;
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Function<ServerCommandSource, Boolean> commandValidator, boolean isRuleApp)
{
super(code, perUser, parent);
this.saveTimeout = 0;
@@ -74,11 +77,13 @@ public class CarpetScriptHost extends ScriptHost
}
appConfig = config;
appArgTypes = argTypes;
this.commandValidator = commandValidator;
this.isRuleApp = isRuleApp;
}
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source)
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source, Function<ServerCommandSource, Boolean> commandValidator, boolean isRuleApp)
{
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>());
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp);
// parse code and convert to expression
if (module != null)
{
@@ -169,7 +174,7 @@ public class CarpetScriptHost extends ScriptHost
@Override
protected ScriptHost duplicate()
{
return new CarpetScriptHost(scriptServer, main, false, this, appConfig, appArgTypes);
return new CarpetScriptHost(scriptServer, main, false, this, appConfig, appArgTypes, commandValidator, isRuleApp);
}
@Override
@@ -739,4 +744,15 @@ public class CarpetScriptHost extends ScriptHost
{
return scriptServer;
}
@Override
public boolean issueDeprecation(String feature)
{
if(super.issueDeprecation(feature))
{
Messenger.m(responsibleSource, "rb '"+feature+"' is deprecated and soon will be removed. Please consult the docs for their replacement");
return true;
}
return false;
}
}
@@ -84,6 +84,8 @@ public class CarpetScriptServer
registerBuiltInScript(BundledModule.carpetNative("math", true));
registerBuiltInScript(BundledModule.carpetNative("chunk_display", false));
registerBuiltInScript(BundledModule.carpetNative("ai_tracker", false));
registerBuiltInScript(BundledModule.carpetNative("draw_beta", false));
registerBuiltInScript(BundledModule.carpetNative("distance_beta", false));
}
public CarpetScriptServer(MinecraftServer server)
@@ -101,7 +103,7 @@ public class CarpetScriptServer
tickStart = 0L;
stopAll = false;
holyMoly = server.getCommandManager().getDispatcher().getRoot().getChildren().stream().map(CommandNode::getName).collect(Collectors.toSet());
globalHost = CarpetScriptHost.create(this, null, false, null);
globalHost = CarpetScriptHost.create(this, null, false, null, p -> true, false);
}
public void initializeForWorld()
@@ -117,7 +119,7 @@ public class CarpetScriptServer
Messenger.m(server.getCommandSource(), "Auto-loading world scarpet apps");
for (String moduleName: listAvailableModules(false))
{
addScriptHost(server.getCommandSource(), moduleName, true, true, null);
addScriptHost(server.getCommandSource(), moduleName, null, true, true, false);
}
}
@@ -168,7 +170,7 @@ public class CarpetScriptServer
{
for (Module mi : bundledModuleData)
{
if (!mi.isLibrary()) moduleNames.add(mi.getName());
if (!mi.isLibrary() && !mi.getName().endsWith("_beta")) moduleNames.add(mi.getName());
}
}
File folder = server.getSavePath(WorldSavePath.ROOT).resolve("scripts").toFile();
@@ -193,10 +195,11 @@ public class CarpetScriptServer
return modules.get(name);
}
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload, Function<ServerCommandSource, Boolean> commandValidator)
public boolean addScriptHost(ServerCommandSource source, String name, Function<ServerCommandSource, Boolean> commandValidator,
boolean perPlayer, boolean autoload, boolean isRuleApp)
{
//TODO add per player modules to support player actions better on a server
boolean isRuleApp = commandValidator != null;
if (commandValidator == null) commandValidator = p -> true;
long start = System.nanoTime();
name = name.toLowerCase(Locale.ROOT);
boolean reload = false;
@@ -212,7 +215,7 @@ public class CarpetScriptServer
Messenger.m(source, "r Failed to add "+name+" app");
return false;
}
CarpetScriptHost newHost = CarpetScriptHost.create(this, module, perPlayer, source);
CarpetScriptHost newHost = CarpetScriptHost.create(this, module, perPlayer, source, commandValidator, isRuleApp);
if (newHost == null)
{
Messenger.m(source, "r Failed to add "+name+" app");
@@ -434,13 +437,26 @@ public class CarpetScriptServer
}
}
static class TransferData
{
boolean perUser;
Function<ServerCommandSource, Boolean> commandValidator;
boolean isRuleApp;
private TransferData(CarpetScriptHost host)
{
perUser = host.perUser;
commandValidator = host.commandValidator;
isRuleApp = host.isRuleApp;
}
}
public void reload(MinecraftServer server)
{
Map<String, Boolean> apps = new HashMap<>();
modules.forEach((s, h) -> apps.put(s, h.perUser));
Map<String, TransferData> apps = new HashMap<>();
modules.forEach((s, h) -> apps.put(s, new TransferData(h)));
apps.keySet().forEach(s -> removeScriptHost(server.getCommandSource(), s, false, false));
CarpetEventServer.Event.clearAllBuiltinEvents();
init();
apps.forEach((s, pp) -> addScriptHost(server.getCommandSource(), s, pp, false, null));
apps.forEach((s, data) -> addScriptHost(server.getCommandSource(), s,data.commandValidator, data.perUser,false, data.isRuleApp));
}
}
+22 -1
View File
@@ -366,7 +366,28 @@ public class Expression
public void alias(String copy, String original)
{
functions.put(copy, functions.get(original));
ILazyFunction originalFunction = functions.get(original);
functions.put(copy, new ILazyFunction()
{
@Override
public int getNumParams()
{
return originalFunction.getNumParams();
}
@Override
public boolean numParamsVaries()
{
return originalFunction.numParamsVaries();
}
@Override
public LazyValue lazyEval(Context c, Integer type, Expression expr, Tokenizer.Token token, List<LazyValue> lazyParams)
{
c.host.issueDeprecation(copy+"(...)");
return originalFunction.lazyEval(c, type, expr, token, lazyParams);
}
});
}
+15 -1
View File
@@ -8,9 +8,11 @@ import carpet.script.value.FunctionValue;
import carpet.script.value.Value;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -34,6 +36,8 @@ public abstract class ScriptHost
private final Map<Value, Object> locks = new ConcurrentHashMap<>();
protected boolean inTermination = false;
private final Set<String> deprecations = new HashSet<>();
public Random getRandom(long aLong)
{
if (randomizers.size() > 1024)
@@ -379,4 +383,14 @@ public abstract class ScriptHost
{
errorSnooper=null;
}
public static final Logger DEPRECATION_LOG = LogManager.getLogger("Scarpet Deprecation Warnings");
public boolean issueDeprecation(String feature)
{
if (deprecations.contains(feature)) return false;
deprecations.add(feature);
DEPRECATION_LOG.warn("'"+feature+"' is deprecated and soon will be removed. Please consult the docs for their replacement");
return true;
}
}
@@ -577,6 +577,7 @@ public class Auxiliary {
expression.addLazyFunction("seed", -1, (c, t, lv) -> {
ServerCommandSource s = ((CarpetContext)c).s;
c.host.issueDeprecation("seed()");
Value ret = new NumericValue(s.getWorld().getSeed());
return (cc, tt) -> ret;
});
@@ -785,6 +786,7 @@ public class Auxiliary {
boolean shared = false;
if (lv.size()>0)
{
c.host.issueDeprecation("load_app_data(...) with arguments");
file = recognizeResource(lv.get(0).evalValue(c));
if (lv.size() > 1)
{
@@ -807,6 +809,7 @@ public class Auxiliary {
boolean shared = false;
if (lv.size()>1)
{
c.host.issueDeprecation("store_app_data(...) with more than one argument");
file = recognizeResource(lv.get(1).evalValue(c));
if (lv.size() > 2)
{
@@ -56,7 +56,7 @@ public class Scoreboards {
}
String objectiveName = lv.get(0).evalValue(c).getString();
ScoreboardObjective objective = scoreboard.getObjective(objectiveName);
if (objective == null) throw new InternalExpressionException("Unknown objective: "+objectiveName);
if (objective == null) return LazyValue.NULL;
if (lv.size()==1)
{
Value ret = ListValue.wrap(scoreboard.getAllPlayerScores(objective).stream().map(s -> new StringValue(s.getPlayerName())).collect(Collectors.toList()));
@@ -143,7 +143,7 @@ public class Scoreboards {
}
String objectiveString = target.getString();
ScoreboardObjective objective = scoreboard.getObjective(objectiveString);
if (objective == null) throw new InternalExpressionException("Objective doesn't exist: "+objectiveString);
if (objective == null) return LazyValue.NULL;
scoreboard.setObjectiveSlot(slot, objective);
return (_c, _t) -> new NumericValue(slot);
});
@@ -543,6 +543,7 @@ public class WorldAccess {
// Deprecated, use loaded_status as more indicative
expression.addLazyFunction("loaded_ep", -1, (c, t, lv) ->
{
c.host.issueDeprecation("loaded_ep(...)");
BlockPos pos = BlockArgument.findIn((CarpetContext)c, lv, 0).block.getPos();
Value retval = ((CarpetContext)c).s.getWorld().getChunkManager().shouldTickChunk(new ChunkPos(pos))?Value.TRUE : Value.FALSE;
return (c_, t_) -> retval;
@@ -739,11 +740,12 @@ public class WorldAccess {
return (c_, t_) -> Value.FALSE;
BlockState finalSourceBlockState = sourceBlockState;
BlockPos targetPos = targetLocator.block.getPos();
Boolean[] result = new Boolean[]{true};
cc.s.getMinecraftServer().submitAndJoin( () ->
{
Clearable.clear(world.getBlockEntity(targetPos));
world.setBlockState(targetPos, finalSourceBlockState, 2);
if (finalData != null)
boolean success = world.setBlockState(targetPos, finalSourceBlockState, 2);
if (success && finalData != null)
{
BlockEntity be = world.getBlockEntity(targetPos);
if (be != null)
@@ -756,7 +758,9 @@ public class WorldAccess {
be.markDirty();
}
}
result[0] = success;
});
if (!result[0]) return LazyValue.FALSE;
Value retval = new BlockValue(finalSourceBlockState, world, targetLocator.block.getPos());
return (c_, t_) -> retval;
});
@@ -962,6 +966,7 @@ public class WorldAccess {
// Deprecated for block_state()
expression.addLazyFunction("property", -1, (c, t, lv) ->
{
c.host.issueDeprecation("property(...)");
BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0);
BlockState state = locator.block.getBlockState();
if (lv.size() <= locator.offset)
@@ -978,6 +983,7 @@ public class WorldAccess {
// Deprecated for block_state()
expression.addLazyFunction("block_properties", -1, (c, t, lv) ->
{
c.host.issueDeprecation("block_properties(...)");
BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0);
BlockState state = locator.block.getBlockState();
StateManager<Block, BlockState> states = state.getBlock().getStateManager();