-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixup API a bit, fix block placing and add block place cmd
- Loading branch information
1 parent
c982e4a
commit d98d187
Showing
23 changed files
with
218 additions
and
108 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
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
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
74 changes: 74 additions & 0 deletions
74
server/src/main/java/com/soulfiremc/server/brigadier/EnumArgumentType.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,74 @@ | ||
/* | ||
* 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.arguments.ArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Collection; | ||
import java.util.Locale; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Stream; | ||
|
||
@RequiredArgsConstructor | ||
public class EnumArgumentType<T extends Enum<T>> implements ArgumentType<T> { | ||
private final Class<T> type; | ||
|
||
@Override | ||
public T parse(StringReader stringReader) throws CommandSyntaxException { | ||
final var start = stringReader.getCursor(); | ||
var string = stringReader.readString(); | ||
|
||
for (var constant : type.getEnumConstants()) { | ||
if (constant.name().equalsIgnoreCase(string)) { | ||
return constant; | ||
} | ||
} | ||
|
||
stringReader.setCursor(start); | ||
throw new SimpleCommandExceptionType(new LiteralMessage("Invalid enum value")) | ||
.createWithContext(stringReader); | ||
} | ||
|
||
@Override | ||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { | ||
final var remaining = builder.getRemaining().toLowerCase(Locale.ROOT); | ||
|
||
for (var constant : type.getEnumConstants()) { | ||
if (constant.name().toLowerCase(Locale.ROOT).startsWith(remaining)) { | ||
builder.suggest(constant.name()); | ||
} | ||
} | ||
|
||
return builder.buildFuture(); | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return Stream.of(type.getEnumConstants()) | ||
.map(Enum::name) | ||
.toList(); | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.