-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f768854
commit db586a2
Showing
8 changed files
with
392 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
server/src/main/java/com/soulfiremc/server/brigadier/ArgumentTypeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.brigadier; | ||
|
||
import com.mojang.brigadier.LiteralMessage; | ||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
|
||
public class ArgumentTypeHelper { | ||
private ArgumentTypeHelper() { | ||
} | ||
|
||
public static void mustReadSpace(StringReader reader) throws CommandSyntaxException { | ||
if (!reader.canRead() || reader.peek() != ' ') { | ||
throw new SimpleCommandExceptionType(new LiteralMessage("Expected space")).createWithContext(reader); | ||
} | ||
|
||
reader.skip(); | ||
} | ||
|
||
public static DoubleAxisData readAxis(StringReader reader) throws CommandSyntaxException { | ||
if (reader.canRead() && reader.peek() == '~') { | ||
reader.skip(); | ||
return new DoubleAxisData(true, reader.readDouble()); | ||
} | ||
|
||
return new DoubleAxisData(false, reader.readDouble()); | ||
} | ||
|
||
public record DoubleAxisData(boolean relative, double value) { | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
server/src/main/java/com/soulfiremc/server/brigadier/DynamicXYZArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.brigadier; | ||
|
||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.cloudburstmc.math.vector.Vector3d; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class DynamicXYZArgumentType implements ArgumentType<DynamicXYZArgumentType.XYZLocationMapper> { | ||
@Override | ||
public XYZLocationMapper parse(StringReader stringReader) throws CommandSyntaxException { | ||
var x = ArgumentTypeHelper.readAxis(stringReader); | ||
ArgumentTypeHelper.mustReadSpace(stringReader); | ||
var y = ArgumentTypeHelper.readAxis(stringReader); | ||
ArgumentTypeHelper.mustReadSpace(stringReader); | ||
var z = ArgumentTypeHelper.readAxis(stringReader); | ||
|
||
return baseLocation -> { | ||
var xValue = x.relative() ? baseLocation.getX() + x.value() : x.value(); | ||
var yValue = y.relative() ? baseLocation.getY() + y.value() : y.value(); | ||
var zValue = z.relative() ? baseLocation.getZ() + z.value() : z.value(); | ||
|
||
return Vector3d.from(xValue, yValue, zValue); | ||
}; | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return List.of("~ ~ ~", "~ ~1 ~", "1 2 3", "~ ~ ~-1", "~ ~ ~0"); | ||
} | ||
|
||
public interface XYZLocationMapper { | ||
Vector3d getAbsoluteLocation(Vector3d baseLocation); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/main/java/com/soulfiremc/server/brigadier/DynamicXZArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.brigadier; | ||
|
||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.cloudburstmc.math.vector.Vector2d; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class DynamicXZArgumentType implements ArgumentType<DynamicXZArgumentType.XZLocationMapper> { | ||
@Override | ||
public XZLocationMapper parse(StringReader stringReader) throws CommandSyntaxException { | ||
var x = ArgumentTypeHelper.readAxis(stringReader); | ||
ArgumentTypeHelper.mustReadSpace(stringReader); | ||
var z = ArgumentTypeHelper.readAxis(stringReader); | ||
|
||
return baseLocation -> { | ||
var xValue = x.relative() ? baseLocation.getX() + x.value() : x.value(); | ||
var zValue = z.relative() ? baseLocation.getY() + z.value() : z.value(); | ||
|
||
return Vector2d.from(xValue, zValue); | ||
}; | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return List.of("~ ~", "~ ~1", "1 2", "~ ~-1", "~ ~0"); | ||
} | ||
|
||
public interface XZLocationMapper { | ||
Vector2d getAbsoluteLocation(Vector2d baseLocation); | ||
} | ||
} |
Oops, something went wrong.