Use newer Java features in places where it makes sense and more (#1009)

This commit is contained in:
altrisi authored and GitHub committed 2021-08-03 07:32:40 -03:00
1 parent 28cb136c6b
commit 7fb5e47da5
34 files changed
+338 -399

No files matched your search

@@ -9,7 +9,6 @@ import carpet.script.value.StringValue;
import carpet.script.value.Value;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Arrays;
import java.util.Collections;
@@ -20,7 +19,8 @@ import java.util.Map;
public class EntityEventsGroup
{
private final Map<Event, Map<Pair<String,String>, CarpetEventServer.Callback>> actions;
private static record EventKey(String host, String user) {}
private final Map<Event, Map<EventKey, CarpetEventServer.Callback>> actions;
private final Entity entity;
public EntityEventsGroup(Entity e)
{
@@ -31,22 +31,22 @@ public class EntityEventsGroup
public void onEvent(Event type, Object... args)
{
if (actions.isEmpty()) return; // most of the cases, trying to be nice
Map<Pair<String,String>, CarpetEventServer.Callback> actionSet = actions.get(type);
Map<EventKey, CarpetEventServer.Callback> actionSet = actions.get(type);
if (actionSet == null) return;
if (CarpetServer.scriptServer == null) return; // executed after world is closin down
for (Iterator<Map.Entry<Pair<String,String>, CarpetEventServer.Callback>> iterator = actionSet.entrySet().iterator(); iterator.hasNext(); )
for (Iterator<Map.Entry<EventKey, CarpetEventServer.Callback>> iterator = actionSet.entrySet().iterator(); iterator.hasNext(); )
{
Map.Entry<Pair<String,String>, CarpetEventServer.Callback> action = iterator.next();
Pair<String,String> key = action.getKey();
ScriptHost host = CarpetServer.scriptServer.getAppHostByName(key.getLeft());
Map.Entry<EventKey, CarpetEventServer.Callback> action = iterator.next();
EventKey key = action.getKey();
ScriptHost host = CarpetServer.scriptServer.getAppHostByName(key.host());
if (host == null)
{
iterator.remove();
continue;
}
if (key.getRight() != null)
if (key.user() != null)
{
if (entity.getServer().getPlayerManager().getPlayer(key.getRight())==null)
if (entity.getServer().getPlayerManager().getPlayer(key.user())==null)
{
iterator.remove();
continue;
@@ -60,7 +60,7 @@ public class EntityEventsGroup
public void addEvent(Event type, ScriptHost host, FunctionValue fun, List<Value> extraargs)
{
Pair<String,String> key = Pair.of(host.getName(), host.user);
EventKey key = new EventKey(host.getName(), host.user);
if (fun != null)
{
CarpetEventServer.Callback call = type.create(key, fun, extraargs);
@@ -117,13 +117,13 @@ public class EntityEventsGroup
argcount = args+1; // entity is not extra
byName.put(identifier, this);
}
public CarpetEventServer.Callback create(Pair<String,String> key, FunctionValue function, List<Value> extraArgs)
public CarpetEventServer.Callback create(EventKey key, FunctionValue function, List<Value> extraArgs)
{
if ((function.getArguments().size()-(extraArgs == null ? 0 : extraArgs.size())) != argcount)
{
return null;
}
return new CarpetEventServer.Callback(key.getLeft(), key.getRight(), function, extraArgs);
return new CarpetEventServer.Callback(key.host(), key.user(), function, extraArgs);
}
public CarpetEventServer.CallbackResult call(CarpetEventServer.Callback tickCall, Entity entity, Object ... args)
{