Skip to content

Commit

Permalink
fix #792
Browse files Browse the repository at this point in the history
  • Loading branch information
gamma-delta committed Nov 20, 2024
1 parent 2428d83 commit 2e4d53f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public static void addCreateEventListener(BiConsumer<CastingEnvironment, Compoun

/**
* Add a listener that will be called whenever a new CastingEnvironment is created (legacy).
*
* @deprecated replaced by {@link #addCreateEventListener(BiConsumer)}
*/
@Deprecated(since = "0.11.0-pre-660")
public static void addCreateEventListener(Consumer<CastingEnvironment> listener) {
createEventListeners.add((env, data) -> {listener.accept(env);});
createEventListeners.add((env, data) -> {
listener.accept(env);
});
}

private boolean createEventTriggered = false;
Expand All @@ -77,7 +80,8 @@ public final void triggerCreateEvent(CompoundTag userData) {

protected final ServerLevel world;

protected Map<CastingEnvironmentComponent.Key<?>, @NotNull CastingEnvironmentComponent> componentMap = new HashMap<>();
protected Map<CastingEnvironmentComponent.Key<?>, @NotNull CastingEnvironmentComponent> componentMap =
new HashMap<>();
private final List<PostExecution> postExecutions = new ArrayList<>();

private final List<PostCast> postCasts = new ArrayList<>();
Expand All @@ -104,16 +108,20 @@ public int maxOpCount() {
* <p>
* Implementations should NOT rely on this in general, use the methods on this class instead.
* This is mostly for spells (flight, etc)
*
* @deprecated as of build 0.11.1-7-pre-619 you are recommended to use {@link #getCastingEntity}
*/
@Deprecated(since="0.11.1-7-pre-619")
@Deprecated(since = "0.11.1-7-pre-619")
@Nullable
public ServerPlayer getCaster() {
return getCastingEntity() instanceof ServerPlayer sp ? sp : null;
};
}

;

/**
* Gets the caster. Can be null if {@link #getCaster()} is also null
*
* @return the entity casting
*/
@Nullable
Expand Down Expand Up @@ -299,7 +307,12 @@ public final boolean isVecInAmbit(Vec3 vec) {
}

public final boolean isEntityInRange(Entity e) {
return (e instanceof Player && HexConfig.server().trueNameHasAmbit()) || (this.isVecInWorld(e.position()) && this.isVecInRange(e.position()));
return isEntityInRange(e, false);
}

public final boolean isEntityInRange(Entity e, boolean ignoreTruenameAmbit) {
boolean truenameCheat = !ignoreTruenameAmbit && (e instanceof Player && HexConfig.server().trueNameHasAmbit());
return truenameCheat || (this.isVecInWorld(e.position()) && this.isVecInRange(e.position()));
}

/**
Expand Down Expand Up @@ -361,7 +374,8 @@ public InteractionHand getOtherHand() {
*/
protected abstract List<ItemStack> getUsableStacks(StackDiscoveryMode mode);

protected List<ItemStack> getUsableStacksForPlayer(StackDiscoveryMode mode, @Nullable InteractionHand castingHand, ServerPlayer caster) {
protected List<ItemStack> getUsableStacksForPlayer(StackDiscoveryMode mode, @Nullable InteractionHand castingHand
, ServerPlayer caster) {
return switch (mode) {
case QUERY -> {
var out = new ArrayList<ItemStack>();
Expand All @@ -386,8 +400,8 @@ protected List<ItemStack> getUsableStacksForPlayer(StackDiscoveryMode mode, @Nul
// If we're casting from the main hand, try to pick from the slot one to the right of the selected slot
// Otherwise, scan the hotbar left to right
var anchorSlot = castingHand != InteractionHand.OFF_HAND
? (caster.getInventory().selected + 1) % 9
: 0;
? (caster.getInventory().selected + 1) % 9
: 0;


for (int delta = 0; delta < 9; delta++) {
Expand Down Expand Up @@ -440,7 +454,7 @@ protected List<HeldItemInfo> getPrimaryStacksForPlayer(InteractionHand castingHa
secondaryItem = ItemStack.EMPTY.copy();

return List.of(new HeldItemInfo(secondaryItem, HexUtils.otherHand(castingHand)), new HeldItemInfo(primaryItem,
castingHand));
castingHand));
}

/**
Expand Down Expand Up @@ -537,12 +551,15 @@ public boolean withdrawItem(Predicate<ItemStack> stackOk, int count, boolean act

/**
* Attempt to replace the first stack found which matches the predicate with the stack to replace with.
*
* @return whether it was successful.
*/
public abstract boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand);
public abstract boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith,
@Nullable InteractionHand hand);


public boolean replaceItemForPlayer(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand, ServerPlayer caster) {
public boolean replaceItemForPlayer(Predicate<ItemStack> stackOk, ItemStack replaceWith,
@Nullable InteractionHand hand, ServerPlayer caster) {
if (caster == null)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class OpGetEntitiesBy(val checker: Predicate<Entity>, val negate: Boolean) : Con
}

companion object {
// Ignore truename ambit to fix #792 so you can't slurp up all players in the whole world
fun isReasonablySelectable(ctx: CastingEnvironment, e: Entity) =
ctx.isEntityInRange(e) && e.isAlive && !e.isSpectator
ctx.isEntityInRange(e, true) && e.isAlive && !e.isSpectator

@JvmStatic
fun isAnimal(e: Entity): Boolean = e is Animal || e is WaterAnimal
Expand Down

0 comments on commit 2e4d53f

Please sign in to comment.