Added scarpet team functions (#569)

* Added team functions

* Docs for team functions

* Added team functions to editor keywords

* Added static method getPlayerNameByValue to EntityValue (also works with non existing players

Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com>
This commit is contained in:
replaceitemandgnembon authored and GitHub committed 2020-11-26 02:59:23 -04:00
1 parent 1fb4209a15
commit 210b0fc8ea
6 files changed
+357 -1

No files matched your search

@@ -9,14 +9,20 @@ import carpet.script.value.ListValue;
import carpet.script.value.NullValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.FormattedTextValue;
import carpet.script.value.Value;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.ServerScoreboard;
import net.minecraft.scoreboard.ScoreboardCriterion;
import net.minecraft.scoreboard.ScoreboardObjective;
import net.minecraft.scoreboard.ScoreboardPlayerScore;
import net.minecraft.scoreboard.Team;
import net.minecraft.scoreboard.AbstractTeam;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.stream.Collectors;
@@ -147,5 +153,249 @@ public class Scoreboards {
scoreboard.setObjectiveSlot(slot, objective);
return (_c, _t) -> new NumericValue(slot);
});
expression.addLazyFunction("team_list", -1, (c, t, lv) ->
{
if(lv.size() > 1) throw new InternalExpressionException("'team_list' requires zero or one parameters");
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
if(lv.size() == 0) {
Value ret = ListValue.wrap(scoreboard.getTeamNames().stream().map(StringValue::of).collect(Collectors.toList()));
return (_c, _t) -> ret;
} else if(lv.size() == 1) {
Team team = scoreboard.getTeam(lv.get(0).evalValue(c).getString());
if(team == null) return LazyValue.NULL;
Value ret = ListValue.wrap(team.getPlayerList().stream().map(StringValue::of).collect(Collectors.toList()));
return (_c, _t) -> ret;
} else {
return LazyValue.NULL;
}
});
expression.addLazyFunction("team_add", -1, (c, t, lv) ->
{
if(!(lv.size() < 3 && lv.size() > 0)) throw new InternalExpressionException("'team_add' requires one or two parameters");
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
String teamName = lv.get(0).evalValue(c).getString();
if(lv.size() == 1) {
if(scoreboard.getTeam(teamName) == null) {
scoreboard.addTeam(teamName);
Value ret = new StringValue(teamName);
return (_c, _t) -> ret;
} else {
return LazyValue.NULL;
}
} else if(lv.size() == 2) {
Value playerVal = lv.get(1).evalValue(c);
String player = EntityValue.getPlayerNameByValue(playerVal);
if(player == null) return LazyValue.NULL;
Team team = scoreboard.getTeam(teamName);
if(team == null) return LazyValue.NULL;
if(team.isEqual(scoreboard.getPlayerTeam(player))) {
return LazyValue.FALSE;
} else {
scoreboard.addPlayerToTeam(player,scoreboard.getTeam(teamName));
return LazyValue.TRUE;
}
} else {
return LazyValue.NULL;
}
});
expression.addLazyFunction("team_remove", 1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
Value teamVal = lv.get(0).evalValue(c);
String team = teamVal.getString();
if(scoreboard.getTeam(team) != null) {
scoreboard.removeTeam(scoreboard.getTeam(team));
return LazyValue.TRUE;
} else {
return LazyValue.NULL;
}
});
expression.addLazyFunction("team_leave", 1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
Value playerVal = lv.get(0).evalValue(c);
String player = EntityValue.getPlayerNameByValue(playerVal);
if(player == null) return LazyValue.NULL;
Value ret = new NumericValue(scoreboard.clearPlayerTeam(player));
return (_c, _t) -> ret;
});
expression.addLazyFunction("team_property", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
if(lv.size() < 2 || lv.size() > 3) throw new InternalExpressionException("'team_property' requires two or three arguments");
Value teamVal = lv.get(0).evalValue(c);
Value propertyVal = lv.get(1).evalValue(c);
Value settingVal = null;
boolean modifying = false;
if(lv.size() == 3) {
modifying = true;
settingVal = lv.get(2).evalValue(c);
}
Team team = scoreboard.getTeam(teamVal.getString());
if(team == null) return LazyValue.NULL;
if(!(propertyVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string as the second argument");
switch (propertyVal.getString()) {
case "collision_rule":
if(!modifying) {
Value ret = new StringValue(team.getCollisionRule().name);
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string as the third argument for the property " + propertyVal.getString());
AbstractTeam.CollisionRule collisionRule = AbstractTeam.CollisionRule.getRule(settingVal.getString());
if(collisionRule == null) throw new InternalExpressionException("Unknown value for property " + propertyVal.getString() + ": " + settingVal.getString());
team.setCollisionRule(collisionRule);
break;
case "color":
if(!modifying) {
Value ret = new StringValue(team.getColor().getName());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string as the third argument for the property " + propertyVal.getString());
Formatting color = Formatting.byName(settingVal.getString().toUpperCase());
if(color == null || !color.isColor()) throw new InternalExpressionException("Unknown value for property " + propertyVal.getString() + ": " + settingVal.getString());
team.setColor(color);
break;
case "death_message_visibility":
if(!modifying) {
Value ret = new StringValue(team.getDeathMessageVisibilityRule().name);
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string as the third argument for the property " + propertyVal.getString());
AbstractTeam.VisibilityRule deathMessageVisibility = AbstractTeam.VisibilityRule.getRule(settingVal.getString());
if(deathMessageVisibility == null) throw new InternalExpressionException("Unknown value for property " + propertyVal.getString() + ": " + settingVal.getString());
team.setDeathMessageVisibilityRule(deathMessageVisibility);
break;
case "display_name":
if(!modifying) {
Value ret = new FormattedTextValue(team.getDisplayName());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string or formatted text as the third argument for the property " + propertyVal.getString());
Text displayName;
if(settingVal instanceof FormattedTextValue) {
displayName = ((FormattedTextValue) settingVal).getText();
} else {
displayName = new LiteralText(settingVal.getString());
}
team.setDisplayName(displayName);
break;
case "friendly_fire":
if(!modifying) {
Value ret = new NumericValue(team.isFriendlyFireAllowed());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof NumericValue)) throw new InternalExpressionException("'team_property' requires a boolean as the third argument for the property " + propertyVal.getString());
boolean friendlyFire = settingVal.getBoolean();
team.setFriendlyFireAllowed(friendlyFire);
break;
case "nametag_visibility":
if(!modifying) {
Value ret = new StringValue(team.getNameTagVisibilityRule().name);
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string as the third argument for the property " + propertyVal.getString());
AbstractTeam.VisibilityRule nametagVisibility = AbstractTeam.VisibilityRule.getRule(settingVal.getString());
if(nametagVisibility == null) throw new InternalExpressionException("Unknown value for property " + propertyVal.getString() + ": " + settingVal.getString());
team.setNameTagVisibilityRule(nametagVisibility);
break;
case "prefix":
if(!modifying) {
Value ret = new FormattedTextValue(team.getPrefix());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property ' requires a string or formatted text as the third argument for the property " + propertyVal.getString());
Text prefix;
if(settingVal instanceof FormattedTextValue) {
prefix = ((FormattedTextValue) settingVal).getText();
} else {
prefix = new LiteralText(settingVal.getString());
}
team.setPrefix(prefix);
break;
case "see_friendly_invisibles":
if(!modifying) {
Value ret = new NumericValue(team.shouldShowFriendlyInvisibles());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof NumericValue)) throw new InternalExpressionException("'team_property' requires a boolean as the third argument for the property " + propertyVal.getString());
boolean seeFriendlyInvisibles = settingVal.getBoolean();
team.setShowFriendlyInvisibles(seeFriendlyInvisibles);
break;
case "suffix":
if(!modifying) {
Value ret = new FormattedTextValue(team.getSuffix());
return (_c, _t) -> ret;
}
if(!(settingVal instanceof StringValue)) throw new InternalExpressionException("'team_property' requires a string or formatted text as the third argument for the property " + propertyVal.getString());
Text suffix;
if(settingVal instanceof FormattedTextValue) {
suffix = ((FormattedTextValue) settingVal).getText();
} else {
suffix = new LiteralText(settingVal.getString());
}
team.setSuffix(suffix);
break;
default:
throw new InternalExpressionException("team property " + propertyVal.getString() + " is not a valid property");
}
return LazyValue.TRUE;
});
}
}