Skip to content

Commit

Permalink
feat: add set home and home command + sonarqube action
Browse files Browse the repository at this point in the history
  • Loading branch information
PasVegan committed Apr 22, 2024
1 parent 181405f commit d9cee9e
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 16 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: Build and analyze
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages
uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Build and analyze
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: ./gradlew build sonar --info
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ run/
img/

# ignore github workflow for now, might use later
.github/
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'fabric-loom' version '1.5.1'
id 'maven-publish'
id "org.sonarqube" version "5.0.0.4638"
}

sourceCompatibility = JavaVersion.VERSION_17
Expand Down Expand Up @@ -82,3 +83,10 @@ publishing {
// retrieving dependencies.
}
}

sonar {
properties {
property "sonar.projectKey", "PasVegan_teamcmd_74e72b11-6df8-4ab9-aaf3-6260773756be"
property "sonar.projectName", "teamcmd"
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.7

# Mod Properties
mod_version = 1.1.3
maven_group = io.github.beabfc
mod_version = 2.0.0
maven_group = io.github.PasVegan
archives_base_name = teamcommand

# Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.beabfc.teamcmd;
package io.github.PasVegan.teamcmd;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
Expand All @@ -14,12 +14,15 @@

import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.TeamArgumentType;
import net.minecraft.registry.RegistryKey;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.Team;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;

import java.util.Collection;

Expand Down Expand Up @@ -51,6 +54,8 @@ public class CommandBuilder {
new SimpleCommandExceptionType(Text.translatable("commands.teamcmd.fail.not_invited"));
private static final SimpleCommandExceptionType DUPLICATE_COLOR =
new SimpleCommandExceptionType(Text.translatable("commands.teamcmd.fail.duplicate_color"));
private static final SimpleCommandExceptionType NO_HOME_SET =
new SimpleCommandExceptionType(Text.translatable("commands.teamcmd.fail.tp_to_home"));
private static final DynamicCommandExceptionType TEAM_NOT_FOUND =
new DynamicCommandExceptionType(option -> Text.translatable("team.notFound", option));

Expand All @@ -66,14 +71,16 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
TeamArgumentType.getTeam(ctx, "team")))))
.then(literal("leave").executes(ctx -> executeLeave(ctx.getSource())))
.then(literal("invite").then(argument("player", EntityArgumentType.player()).executes(ctx -> executeInvitePlayer(ctx.getSource(), EntityArgumentType.getPlayer(ctx, "player")))))
.then(literal("accept").executes(ctx -> executeAcceptInvite(ctx.getSource())));
.then(literal("accept").executes(ctx -> executeAcceptInvite(ctx.getSource())))
.then(literal("home").executes(ctx -> executeGoHome(ctx.getSource())));

LiteralArgumentBuilder<ServerCommandSource> setCommand = literal("set")
.then(literal("color").then(argument("color", ColorArgumentType.color()).executes(ctx -> executeSetColor(ctx.getSource(), ColorArgumentType.getColor(ctx, "color")))))
.then(literal("friendlyFire").then(argument("allowed", BoolArgumentType.bool()).executes(ctx -> executeSetFriendlyFire(ctx.getSource(), BoolArgumentType.getBool(ctx, "allowed")))))

.then(literal("seeInvisibles").then(argument("allowed", BoolArgumentType.bool()).executes(ctx -> executeSetShowFriendlyInvisibles(ctx.getSource(), BoolArgumentType.getBool(ctx, "allowed")))))
.then(literal("displayName").then(argument("displayName", StringArgumentType.word()).executes(ctx -> executeSetDisplayName(ctx.getSource(), StringArgumentType.getString(ctx, "displayName")))));
.then(literal("displayName").then(argument("displayName", StringArgumentType.word()).executes(ctx -> executeSetDisplayName(ctx.getSource(), StringArgumentType.getString(ctx, "displayName")))))
.then(literal("home").executes(ctx -> executeSetHome(ctx.getSource())));

teamCmd.then(setCommand);
dispatcher.register(teamCmd);
Expand Down Expand Up @@ -240,7 +247,11 @@ private static int executeLeave(ServerCommandSource source) throws CommandSyntax
TeamUtil.sendToTeammates(player, Text.translatable("commands.teamcmd.teammates.left",
player.getDisplayName()));
player.getScoreboard().clearPlayerTeam(player.getEntityName());
if (team.getPlayerList().size() == 0) player.getScoreboard().removeTeam(team);
if (team.getPlayerList().isEmpty()) {
TeamCommand.HOMES.deleteTeamHome(team.getName());
TeamCommand.HOMES.save("teamhomes.dat");
player.getScoreboard().removeTeam(team);
}
source.sendFeedback(() -> Text.translatable("commands.teamcmd.left", team.getFormattedName()), false);
return 1;
}
Expand Down Expand Up @@ -268,6 +279,54 @@ private static int executeListTeams(ServerCommandSource source) {
return collection.size();
}

private static int executeSetHome(ServerCommandSource source) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
Team team = (Team) player.getScoreboardTeam();
int dimension;
if (team == null) {
throw NOT_IN_TEAM.create();
}

TeamUtil.sendToTeammates(player, Text.translatable("commands.teamcmd.teamates.created_home",
player.getDisplayName()));
RegistryKey<World> dimensionType = player.getWorld().getRegistryKey();
if (dimensionType.equals(World.OVERWORLD)) {
dimension = 0;
} else if (dimensionType.equals(World.NETHER)) {
dimension = 1;
} else if (dimensionType.equals(World.END)) {
dimension = 2;
} else {
dimension = 0;
}
TeamCommand.HOMES.setHome(team.getName(), player.getPos(), player.getRotationClient(), dimension);
TeamCommand.HOMES.save("teamhomes.dat");
source.sendFeedback(() -> Text.translatable("commands.teamcmd.created_home"), false);
return 1;
}

private static int executeGoHome(ServerCommandSource source) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
Team team = (Team) player.getScoreboardTeam();
ServerWorld world;

if (team == null) {
throw NOT_IN_TEAM.create();
}
TeamHomes.TeamHome home = TeamCommand.HOMES.getHome(team.getName());
if (home == null) {
throw NO_HOME_SET.create();
}
switch (home.dimension) {
case 1 -> world = source.getServer().getWorld(World.NETHER);
case 2 -> world = source.getServer().getWorld(World.END);
default -> world = source.getServer().getWorld(World.OVERWORLD);
}
player.teleport(world, home.posX, home.posY, home.posZ, home.yaw, home.pitch);
source.sendFeedback(() -> Text.translatable("commands.teamcmd.tp_to_home"), false);
return 1;
}

private static boolean duplicateName(Collection<Team> teams, String name) {
return !TeamCommand.CONFIG.allowDuplicateDisplaynames && teams.stream().anyMatch(other -> other
.getDisplayName()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.beabfc.teamcmd;
package io.github.PasVegan.teamcmd;

import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.beabfc.teamcmd;
package io.github.PasVegan.teamcmd;

import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;

public class TeamCommand implements DedicatedServerModInitializer {
public static final Config CONFIG = Config.load("teamcommand.toml");
public static final TeamHomes HOMES = TeamHomes.load("teamhomes.dat");

@Override
public void onInitializeServer() {
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/io/github/PasVegan/teamcmd/TeamHomes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.github.PasVegan.teamcmd;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;

import java.io.*;
import java.util.HashMap;

public class TeamHomes implements Serializable {
private final HashMap<String, TeamHome> homes = new HashMap<>();

public static TeamHomes load(String fileName) {
try {
File dataFile = FabricLoader.getInstance().getConfigDir().resolve(fileName).toFile();
if (dataFile.exists()) {
FileInputStream fileIn = new FileInputStream(dataFile);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
TeamHomes teamHomes = (TeamHomes) objectIn.readObject();
objectIn.close();
fileIn.close();
return teamHomes;
} else {
return new TeamHomes();
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public void save(String fileName) {
try {
File dataFile = FabricLoader.getInstance().getConfigDir().resolve(fileName).toFile();
FileOutputStream fileOut = new FileOutputStream(dataFile);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(this);
objectOut.close();
fileOut.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

public TeamHome getHome(String teamName) {
return homes.get(teamName);
}

public void setHome(String teamName, Vec3d position, Vec2f rotation, int dimension) {
TeamHome home = new TeamHome();
home.posX = position.x;
home.posY = position.y;
home.posZ = position.z;
home.pitch = rotation.x;
home.yaw = rotation.y;
home.dimension = dimension;
homes.put(teamName, home);
}

public void deleteTeamHome(String teamName) {
homes.remove(teamName);
}

public static class TeamHome implements Serializable {
public double posX;
public double posY;
public double posZ;
public float yaw;
public float pitch;
public int dimension;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.beabfc.teamcmd;
package io.github.PasVegan.teamcmd;

import net.minecraft.scoreboard.Team;
import net.minecraft.server.MinecraftServer;
Expand All @@ -22,7 +22,6 @@ public static void sendToTeammates(ServerPlayerEntity player, Text message) {
otherPlayer.sendMessage(message);
}
}

}

public static void addInvite(ServerPlayerEntity player, String teamName) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/data/teamcmd/lang/de_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"commands.teamcmd.left": "Du hast das Team %s verlassen",
"commands.teamcmd.teammates.left": "%s hat dein team verlassen",
"commands.teamcmd.invite_expired": "Deine Einladung dem Team %s beizutreten ist ausgelaufen",
"commands.teamcmd.fail.duplicate_color": "Ein Team mit dieser Farbe existiert bereits"
"commands.teamcmd.fail.duplicate_color": "Ein Team mit dieser Farbe existiert bereits",
"commands.teamcmd.teamates.created_home": "%s set a new home for your team",
"commands.teamcmd.created_home": "You set a new home for your team",
"commands.teamcmd.tp_to_home": "Teleport to team home",
"commands.teamcmd.fail.tp_to_home": "Your team has no home"
}
6 changes: 5 additions & 1 deletion src/main/resources/data/teamcmd/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"commands.teamcmd.left": "You left the team %s",
"commands.teamcmd.teammates.left": "%s left your team",
"commands.teamcmd.invite_expired": "Your invite to join the team %s has expired",
"commands.teamcmd.fail.duplicate_color": "A team with this color already exists"
"commands.teamcmd.fail.duplicate_color": "A team with this color already exists",
"commands.teamcmd.teamates.created_home": "%s set a new home for your team",
"commands.teamcmd.created_home": "You set a new home for your team",
"commands.teamcmd.tp_to_home": "Teleport to team home",
"commands.teamcmd.fail.tp_to_home": "Your team has no home"
}
19 changes: 19 additions & 0 deletions src/main/resources/data/teamcmd/lang/fr_fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"commands.teamcmd.fail.in_team": "Vous êtes déjà dans une équipe",
"commands.teamcmd.fail.no_team": "Vous n'êtes dans aucune équipe",
"commands.teamcmd.fail.already_teammate": "Ce joueur est déjà dans votre équipe",
"commands.teamcmd.fail.not_invited": "Vous n'êtes invité dans aucune équipe",
"commands.teamcmd.invite.success": "%s à été invité a rejoindre votre équipe",
"commands.teamcmd.invite": "%s vous à invité a rejoindre l'équipe %s",
"commands.teamcmd.teammates.invite": "%s à invité %s dans votre équipe",
"commands.teamcmd.joined": "Vous avez rejoint l'équipe %s",
"commands.teamcmd.teammates.joined": "%s à rejoint votre équipe",
"commands.teamcmd.left": "Vous avez quitté l'équipe %s",
"commands.teamcmd.teammates.left": "%s à quitté votre équipe",
"commands.teamcmd.invite_expired": "Votre invitation pour rejoindre l'équipe %s à expiré",
"commands.teamcmd.fail.duplicate_color": "Une équipe avec cette couleur existe déjà",
"commands.teamcmd.teamates.created_home": "%s à défini une nouvelle demeure pour votre équipe",
"commands.teamcmd.created_home": "Vous avez défini une nouvelle demeure pour votre équipe",
"commands.teamcmd.tp_to_home": "Téléportation à la demeure de votre équipe",
"commands.teamcmd.fail.tp_to_home": "Votre équipe n'a pas de demeure"
}
6 changes: 3 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"name": "TeamCommand",
"description": "A command for players to create and manage teams. Similar to factions but based on Vanilla scoreboard teams.",
"authors": [
"beabfc"
"PasVegan"
],
"contact": {
"homepage": "",
"sources": "https://github.com/beabfc/teamcommand"
"sources": "https://github.com/PasVegan/teamcmd"
},

"license": "MIT",
Expand All @@ -19,7 +19,7 @@
"environment": "server",
"entrypoints": {
"server": [
"io.github.beabfc.teamcmd.TeamCommand"
"io.github.PasVegan.teamcmd.TeamCommand"
]
},
"mixins": [
Expand Down

0 comments on commit d9cee9e

Please sign in to comment.