Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Dispenser Behavior addition to station-items-api-v0 #99

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.modificationstation.sltest.dispenser;


import net.mine_diver.unsafeevents.listener.EventListener;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.modificationstation.stationapi.api.item.DispenseEvent;
import net.modificationstation.stationapi.api.item.DispenseUtil;

public class DispenserListener {
@EventListener
public static void changeDispenseBehavior(DispenseEvent event) {
DispenseUtil util = event.dispenseUtil;
if (util.itemStack != null && util.inventory != null) {
// Make arrows drop as an item instead of shoot as an entity
if (util.itemStack.itemId == Item.ARROW.id) {
util.shootItemStack(new ItemStack(Item.ARROW));
event.cancel();
}

int targetX = util.x + util.xDir;
int targetY = util.y;
int targetZ = util.z + util.zDir;

// Make buckets pickup liquids
if (util.itemStack.itemId == Item.BUCKET.id) {
if (util.world.method_1779(targetX, targetY, targetZ) == Material.WATER && util.world.getBlockMeta(targetX, targetY, targetZ) == 0) {
util.world.setBlock(targetX, targetY, targetZ, 0);
util.setItem(new ItemStack(Item.WATER_BUCKET));
event.cancel();
}

if (util.world.method_1779(targetX, targetY, targetZ) == Material.LAVA && util.world.getBlockMeta(targetX, targetY, targetZ) == 0) {
util.world.setBlock(targetX, targetY, targetZ, 0);
util.setItem(new ItemStack(Item.LAVA_BUCKET));
event.cancel();
}
}

// Make water buckets place water
if (util.itemStack.itemId == Item.WATER_BUCKET.id) {
if (util.world.method_1779(targetX, targetY, targetZ) == Material.AIR) {
util.world.setBlockStateWithNotify(targetX, targetY, targetZ, Block.WATER.getDefaultState());
util.setItem(new ItemStack(Item.BUCKET));
event.cancel();
}
}

// Make lava buckets place lava
if (util.itemStack.itemId == Item.LAVA_BUCKET.id) {
if (util.world.method_1779(targetX, targetY, targetZ) == Material.AIR) {
util.world.setBlockStateWithNotify(targetX, targetY, targetZ, Block.LAVA.getDefaultState());
util.setItem(new ItemStack(Item.BUCKET));
event.cancel();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public IndispensableBlockItem(int i) {
public void dispense(DispenseUtil util) {
BlockPos pos = util.getFacingBlockPos();
if (util.world.getBlockId(pos.x, pos.y, pos.z) == 0) {
util.decrementItem(1);
util.world.setBlock(pos.x, pos.y, pos.z, Blocks.INDISPENSABLE_BLOCK.get().id);
util.world.playSound((float)pos.x + 0.5f, (float)pos.y + 0.5f, (float)pos.z + 0.5f, Blocks.INDISPENSABLE_BLOCK.get().soundGroup.getSound(), (Blocks.INDISPENSABLE_BLOCK.get().soundGroup.method_1976() + 1.0f) / 2.0f, Blocks.INDISPENSABLE_BLOCK.get().soundGroup.method_1977() * 0.8f);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"net.modificationstation.sltest.item.tool.ToolListener",
"net.modificationstation.sltest.datafixer.DataFixerListener",
"net.modificationstation.sltest.worldgen.TestWorldgenListener",
"net.modificationstation.sltest.bonemeal.BonemealListener"
"net.modificationstation.sltest.bonemeal.BonemealListener",
"net.modificationstation.sltest.dispenser.DispenserListener"
],
"stationapi:event_bus_client": [
"net.modificationstation.sltest.gui.GuiListener",
Expand Down
5 changes: 5 additions & 0 deletions station-dispenser-api-v0/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import net.modificationstation.stationapi.gradle.SubprojectHelpers.addModuleDependencies
import net.modificationstation.stationapi.gradle.SubprojectHelpers.getSubprojectVersion

base.archivesName.set("station-dispenser-api-v0")
matthewperiut marked this conversation as resolved.
Show resolved Hide resolved
version = getSubprojectVersion(project, "1.0.0")

addModuleDependencies(project,
"station-api-base"
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package net.modificationstation.stationapi.api.item;

/**
* Intended to be applied to an Item, which provides a custom dispenser behavior
* @author matthewperiut
* @see DispenseUtil
*/
public interface CustomDispenseBehavior {
void dispense(DispenseUtil util);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.modificationstation.stationapi.api.item;

import lombok.experimental.SuperBuilder;
import net.mine_diver.unsafeevents.Event;
import net.mine_diver.unsafeevents.event.Cancelable;
import net.mine_diver.unsafeevents.event.EventPhases;
import net.modificationstation.stationapi.api.StationAPI;

/**
* Event that gets called when dispenser is activated, precedes and overrides CustomDispenseBehavior.
* @author matthewperiut
* @see CustomDispenseBehavior
* @see DispenseUtil
*/
@Cancelable
@SuperBuilder
@EventPhases(StationAPI.INTERNAL_PHASE)
public class DispenseEvent extends Event {
public final DispenseUtil dispenseUtil;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

import java.util.Random;

/**
* Utility class for changing dispenser behavior, providing tools to customize behavior
* @author matthewperiut
* @see DispenseEvent
* @see CustomDispenseBehavior
*/
public class DispenseUtil {
matthewperiut marked this conversation as resolved.
Show resolved Hide resolved
public World world;
public DispenserBlockEntity dispenserBlockEntity;
Expand Down Expand Up @@ -37,18 +43,23 @@ public DispenseUtil(World world, ItemStack itemStack, DispenserBlockEntity dispe
}

public void decrementItem(int amount) {
if (inventory[slot] != null) {
if (inventory[slot].count - amount <= 0) {
inventory[slot] = null;
} else {
shotItemStack = inventory[slot].split(amount);
if (inventory != null) {
if (inventory[slot] != null) {
if (inventory[slot].count - amount <= 0) {
inventory[slot] = null;
} else {
shotItemStack = inventory[slot].split(amount);
}
dispenserBlockEntity.markDirty();
}
dispenserBlockEntity.markDirty();
}
}

public void setItem(ItemStack itemStack) {
inventory[slot] = itemStack;
if (inventory != null) {
inventory[slot] = itemStack;
dispenserBlockEntity.markDirty();
}
}

private void populateData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import net.minecraft.block.entity.DispenserBlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.modificationstation.stationapi.api.StationAPI;
import net.modificationstation.stationapi.api.item.CustomDispenseBehavior;
import net.modificationstation.stationapi.api.item.DispenseEvent;
import net.modificationstation.stationapi.api.item.DispenseUtil;
import net.modificationstation.stationapi.impl.dispenser.DispenserInfoStorage;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -31,20 +33,19 @@ private void dispense(World world, int x, int y, int z, Random random, CallbackI
int slot = DispenserInfoStorage.slot;
ItemStack[] inventory = DispenserInfoStorage.inventory;

// non-invasive restore
if (inventory != null) {
if (inventory[slot] == null) {
inventory[slot] = currentItemStack;
} else {
inventory[slot].count++;
}
}
DispenseUtil util = new DispenseUtil(world, currentItemStack, dispenserBlockEntity, inventory, slot);


if (currentItemStack != null) {
if (currentItemStack.getItem() instanceof CustomDispenseBehavior behavior) {
behavior.dispense(new DispenseUtil(world, currentItemStack, dispenserBlockEntity, inventory, slot));
ci.cancel();
if (StationAPI.EVENT_BUS.post(
DispenseEvent.builder().dispenseUtil(util).build()
).isCanceled()) {
ci.cancel();
}
else {
if (currentItemStack != null) {
if (currentItemStack.getItem() instanceof CustomDispenseBehavior behavior) {
behavior.dispense(util);
ci.cancel();
}
}
}
matthewperiut marked this conversation as resolved.
Show resolved Hide resolved
matthewperiut marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down