-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add anti-footgun entity selector limits
hi yes I still have commit access lmao enjoy (also idea automatically sorted all the mixins lol)
- Loading branch information
Showing
6 changed files
with
135 additions
and
35 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
35 changes: 35 additions & 0 deletions
35
src/main/java/net/modfest/fireblanket/mixin/footgun/MixinEntitySelectorOptions.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,35 @@ | ||
package net.modfest.fireblanket.mixin.footgun; | ||
|
||
import net.minecraft.command.EntitySelectorOptions; | ||
import net.minecraft.command.EntitySelectorReader; | ||
import net.minecraft.text.Text; | ||
import net.modfest.fireblanket.mixinsupport.ForceableArgument; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
@Mixin(EntitySelectorOptions.class) | ||
public abstract class MixinEntitySelectorOptions { | ||
@Shadow private static void putOption(String id, EntitySelectorOptions.SelectorHandler handler, Predicate<EntitySelectorReader> condition, Text description) { | ||
throw new IllegalStateException("Unimplemented mixin"); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Shadow @Final private static Map OPTIONS; | ||
|
||
@Inject(method = "register", at = @At("TAIL")) | ||
private static void injectForce(CallbackInfo info) { | ||
if (!OPTIONS.containsKey("force")) { | ||
putOption("force", reader -> { | ||
reader.setLocalWorldOnly(); | ||
((ForceableArgument) reader).setForced(reader.getReader().readBoolean()); | ||
}, reader -> !((ForceableArgument) reader).isForced(), Text.translatable("argument.entity.options.force.description")); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/net/modfest/fireblanket/mixin/footgun/MixinEntitySelectorReader.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 @@ | ||
package net.modfest.fireblanket.mixin.footgun; | ||
|
||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; | ||
import net.minecraft.command.EntitySelector; | ||
import net.minecraft.command.EntitySelectorReader; | ||
import net.minecraft.predicate.NumberRange; | ||
import net.minecraft.text.Text; | ||
import net.modfest.fireblanket.mixinsupport.ForceableArgument; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
/** | ||
* Prevents foot-gunning by using an unlimited @e selector without forcing to assure you know what you're doing | ||
*/ | ||
@Mixin(EntitySelectorReader.class) | ||
public class MixinEntitySelectorReader implements ForceableArgument { | ||
@Shadow private boolean includesNonPlayers; | ||
@Shadow private int limit; | ||
@Shadow private NumberRange.DoubleRange distance; | ||
private boolean forced = false; | ||
|
||
private static final DynamicCommandExceptionType LIMIT_UNFORCED = new DynamicCommandExceptionType( | ||
count -> Text.stringifiedTranslatable("argument.entity.selector.limit.unforced", count) | ||
); | ||
|
||
@Override | ||
public void setForced(boolean forced) { | ||
this.forced = forced; | ||
} | ||
|
||
@Override | ||
public boolean isForced() { | ||
return forced; | ||
} | ||
|
||
@Inject(method = "read", at = @At("RETURN")) | ||
private void preventFootgun(CallbackInfoReturnable<EntitySelector> info) throws CommandSyntaxException { | ||
if (this.includesNonPlayers | ||
&& (this.limit > 50 || this.distance == NumberRange.DoubleRange.ANY) | ||
&& !forced) { | ||
throw LIMIT_UNFORCED.create(this.limit); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/net/modfest/fireblanket/mixinsupport/ForceableArgument.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,6 @@ | ||
package net.modfest.fireblanket.mixinsupport; | ||
|
||
public interface ForceableArgument { | ||
void setForced(boolean force); | ||
boolean isForced(); | ||
} |
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,4 @@ | ||
{ | ||
"argument.entity.selector.limit.unforced": "Targeting too many entities with no distance filter; add `force=true` to the selector to bypass", | ||
"argument.entity.options.force.description": "Force-allow more than 50 entities targeted with no distance filter" | ||
} |
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