Skip to content

Commit

Permalink
Merge pull request #56 from haykam821/update-1.21.2
Browse files Browse the repository at this point in the history
Update mod to Minecraft 1.21.2
  • Loading branch information
Patbox authored Nov 5, 2024
2 parents d635c38 + 8c3d348 commit fa9baf2
Show file tree
Hide file tree
Showing 104 changed files with 845 additions and 675 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ The simplest usage of the mod involves registering global events without a filte
registered through `Stimuli.global()`.

```java
Stimuli.global().listen(PlayerChatEvent.EVENT, (sender, message) -> {
sender.sendMessage(new LiteralText("You shall not speak!"), false);
return ActionResult.FAIL;
Stimuli.global().listen(PlayerChatEvent.EVENT, (sender, message, messageType) -> {
sender.sendMessage(Text.literal("You shall not speak!"), false);
return EventResult.DENY;
});
```

This example registers a listener to the `PlayerChatEvent` event and returns `ActionResult.FAIL` in order to cancel
This example registers a listener to the `PlayerChatEvent` event and returns `EventResult.DENY` in order to cancel
further processing of the chat event.

If an event you need is not currently implemented, please consider [submitting a Pull Request](https://github.com/NucleoidMC/stimuli/compare)!
Expand All @@ -49,7 +49,7 @@ EventFilter filter = EventFilter.box(World.OVERWORLD, new BlockPos(0, 0, 0), new

// create a map of event type to listener & deny block breaking
EventListenerMap listeners = new EventListenerMap();
listeners.listen(BlockBreakEvent.EVENT, (player, pos) -> ActionResult.FAIL);
listeners.listen(BlockBreakEvent.EVENT, (player, world, pos) -> EventResult.DENY);

// register our event listener selector so that our events get called
Stimuli.registerSelector(new SimpleListenerSelector(filter, listeners));
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.6.+'
id 'fabric-loom' version '1.8.+'
id 'maven-publish'
}

Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G

minecraft_version=1.21
yarn_mappings=1.21+build.1
loader_version=0.15.11
minecraft_version=1.21.2
yarn_mappings=1.21.2+build.1
loader_version=0.16.7

#Fabric api
fabric_version=0.100.1+1.21
fabric_version=0.106.1+1.21.2

# Mod Properties
mod_version=0.4.12
mod_version=0.5.0
maven_group=xyz.nucleoid
archives_base_name=stimuli
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
7 changes: 5 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/xyz/nucleoid/stimuli/StimuliInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.block.BlockBreakEvent;
import xyz.nucleoid.stimuli.event.block.BlockUseEvent;
import xyz.nucleoid.stimuli.event.entity.EntityUseEvent;
Expand All @@ -27,8 +27,8 @@ public void onInitialize() {
try (var invokers = Stimuli.select().forEntityAt(player, entity.getBlockPos())) {
var result = invokers.get(EntityUseEvent.EVENT)
.onUse(serverPlayer, entity, hand, hit);
if (result != ActionResult.PASS) {
return result;
if (result != EventResult.PASS) {
return result.asActionResult();
}
}
}
Expand All @@ -42,7 +42,7 @@ public void onInitialize() {
return invokers.get(ItemUseEvent.EVENT).onUse(serverPlayer, hand);
}
}
return TypedActionResult.pass(ItemStack.EMPTY);
return ActionResult.PASS;
});

UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
Expand All @@ -57,7 +57,7 @@ public void onInitialize() {
PlayerBlockBreakEvents.BEFORE.register((world, player, pos, state, entity) -> {
if (player instanceof ServerPlayerEntity serverPlayer) {
try (var invokers = Stimuli.select().forEntityAt(player, pos)) {
return invokers.get(BlockBreakEvent.EVENT).onBreak(serverPlayer, (ServerWorld) world, pos) != ActionResult.FAIL;
return invokers.get(BlockBreakEvent.EVENT).onBreak(serverPlayer, (ServerWorld) world, pos) != EventResult.DENY;
}
}
return true;
Expand All @@ -66,7 +66,7 @@ public void onInitialize() {
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (player instanceof ServerPlayerEntity serverPlayer) {
try (var invokers = Stimuli.select().forEntityAt(player, entity.getBlockPos())) {
return invokers.get(PlayerAttackEntityEvent.EVENT).onAttackEntity(serverPlayer, hand, entity, hitResult);
return invokers.get(PlayerAttackEntityEvent.EVENT).onAttackEntity(serverPlayer, hand, entity, hitResult).asActionResult();
}
}
return ActionResult.PASS;
Expand All @@ -75,7 +75,7 @@ public void onInitialize() {
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> {
try (var invokers = Stimuli.select().forEntity(sender)) {
var result = invokers.get(PlayerChatEvent.EVENT).onSendChatMessage(sender, message, params);
return result != ActionResult.FAIL;
return result != EventResult.DENY;
}
});

Expand All @@ -86,7 +86,7 @@ public void onInitialize() {
}
try (var invokers = Stimuli.select().forCommandSource(source)) {
var result = invokers.get(PlayerChatEvent.EVENT).onSendChatMessage(player, message, params);
return result != ActionResult.FAIL;
return result != EventResult.DENY;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package xyz.nucleoid.stimuli.duck;

public interface ExplosionCancellable {
boolean stimuli$isCancelled();
}
43 changes: 43 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/event/DroppedItemsResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package xyz.nucleoid.stimuli.event;

import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.List;

public final class DroppedItemsResult {
private static final DroppedItemsResult DENY = new DroppedItemsResult(EventResult.PASS, Collections.emptyList());

private final EventResult result;
private final List<ItemStack> dropStacks;

private DroppedItemsResult(EventResult result, List<ItemStack> dropStacks) {
this.result = result;
this.dropStacks = dropStacks;
}

public EventResult result() {
return this.result;
}

public List<ItemStack> dropStacks() {
return this.dropStacks;
}

@Override
public String toString() {
return "DroppedItemsResult{result=" + this.result + ", dropStacks=" + this.dropStacks + "}";
}

public static DroppedItemsResult pass(List<ItemStack> dropStacks) {
return new DroppedItemsResult(EventResult.PASS, dropStacks);
}

public static DroppedItemsResult allow(List<ItemStack> dropStacks) {
return new DroppedItemsResult(EventResult.ALLOW, dropStacks);
}

public static DroppedItemsResult deny() {
return DENY;
}
}
29 changes: 29 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/event/EventResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package xyz.nucleoid.stimuli.event;

import net.minecraft.util.ActionResult;

/**
* Represents the change in control flow that should occur in response to an event listener.
*/
public enum EventResult {
/**
* Indicates that the event should move on to the next listener.
*/
PASS,
/**
* Indicates that the event should cancel further processing and allow the behavior to occur.
*/
ALLOW,
/**
* Indicates that the event should cancel further processing and prevent behavior from occurring.
*/
DENY;

public ActionResult asActionResult() {
return switch (this) {
case ALLOW -> ActionResult.SUCCESS;
case DENY -> ActionResult.FAIL;
default -> ActionResult.PASS;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.StimulusEvent;

/**
* Called when any {@link ServerPlayerEntity} attempts to break a block.
*
* <p>Upon return:
* <ul>
* <li>{@link ActionResult#SUCCESS} cancels further processing and allows the break.
* <li>{@link ActionResult#FAIL} cancels further processing and cancels the break.
* <li>{@link ActionResult#PASS} moves on to the next listener.</ul>
* <li>{@link EventResult#ALLOW} cancels further processing and allows the break.
* <li>{@link EventResult#DENY} cancels further processing and cancels the break.
* <li>{@link EventResult#PASS} moves on to the next listener.</ul>
* <p>
* If all listeners return {@link ActionResult#PASS}, the break succeeds.
* If all listeners return {@link EventResult#PASS}, the break succeeds.
*/
public interface BlockBreakEvent {
StimulusEvent<BlockBreakEvent> EVENT = StimulusEvent.create(BlockBreakEvent.class, ctx -> (player, world, pos) -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onBreak(player, world, pos);
if (result != ActionResult.PASS) {
if (result != EventResult.PASS) {
return result;
}
}
} catch (Throwable t) {
ctx.handleException(t);
}
return ActionResult.PASS;
return EventResult.PASS;
});

ActionResult onBreak(ServerPlayerEntity player, ServerWorld world, BlockPos pos);
EventResult onBreak(ServerPlayerEntity player, ServerWorld world, BlockPos pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,45 @@
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.stimuli.event.DroppedItemsResult;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.StimulusEvent;

import java.util.List;

/**
* Called when a block is broken and it tries to drop its items from a loot table.
* <p>
* Listeners can cancel item drops by returning a failure action result such as {@link TypedActionResult#fail(Object)},
* and can additionally modify the items dropped by modifying the returned {@link ItemStack} list.
*
* <p>Upon return:
* <ul>
* <li>{@link DroppedItemsResult#allow(List)} cancels further processing and drops the specified loot.
* <li>{@link DroppedItemsResult#deny()} cancels further processing and drops no loot.
* <li>{@link DroppedItemsResult#pass(List)} moves on to the next listener with the specified loot.</ul>
*
* The drop stacks list is not guaranteed to be mutable, so listeners modifying loot should first copy
* the list before returning it in the result. If the drop stacks list is not modified, it can be passed
* directly to the result.
*/
public interface BlockDropItemsEvent {
StimulusEvent<BlockDropItemsEvent> EVENT = StimulusEvent.create(BlockDropItemsEvent.class, ctx -> (breaker, world, pos, state, dropStacks) -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onDropItems(breaker, world, pos, state, dropStacks);
dropStacks = result.getValue();
if (result.getResult() != ActionResult.PASS) {
dropStacks = result.dropStacks();
if (result.result() != EventResult.PASS) {
return result;
}
}
} catch (Throwable t) {
ctx.handleException(t);
}
return TypedActionResult.pass(dropStacks);
return DroppedItemsResult.pass(dropStacks);
});

TypedActionResult<List<ItemStack>> onDropItems(@Nullable Entity breaker, ServerWorld world, BlockPos pos, BlockState state, List<ItemStack> dropStacks);
/**
* @param dropStacks a list of dropped item stacks, which should be treated as immutable
*/
DroppedItemsResult onDropItems(@Nullable Entity breaker, ServerWorld world, BlockPos pos, BlockState state, List<ItemStack> dropStacks);
}
Loading

0 comments on commit fa9baf2

Please sign in to comment.