Topic category: Help with Minecraft modding (Java Edition)
Procedure #1:
public static void execute(LevelAccessor world, double x, double y, double z) {
double ench = 0;
if (world instanceof Level _level) {
if (!_level.isClientSide()) {
_level.playSound(null, new BlockPos((int) x, (int) y, (int) z),
ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("block.enchantment_table.use")), SoundSource.BLOCKS, 1, 1);
} else {
_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("block.enchantment_table.use")),
SoundSource.BLOCKS, 1, 1, false);
}
}
if (new Object() {
public int getAmount(LevelAccessor world, BlockPos pos, int sltid) {
AtomicInteger _retval = new AtomicInteger(0);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).getCount());
});
}
return _retval.get();
}
}.getAmount(world, new BlockPos((int) x, (int) y, (int) z), 1) > 0
&& EnchantmentHelper.getItemEnchantmentLevel(OtherWorldsModModEnchantments.REAPING, (new Object() {
public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int sltid) {
AtomicReference<ItemStack> _retval = new AtomicReference<>(ItemStack.EMPTY);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).copy());
});
}
return _retval.get();
}
}.getItemStack(world, new BlockPos((int) x, (int) y, (int) z), 0))) > 0
&& EnchantmentHelper.getItemEnchantmentLevel(OtherWorldsModModEnchantments.REAPING, (new Object() {
public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int sltid) {
AtomicReference<ItemStack> _retval = new AtomicReference<>(ItemStack.EMPTY);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).copy());
});
}
return _retval.get();
}
}.getItemStack(world, new BlockPos((int) x, (int) y, (int) z), 0))) <= 6) {
ench = EnchantmentHelper.getItemEnchantmentLevel(OtherWorldsModModEnchantments.REAPING, (new Object() {
public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int sltid) {
AtomicReference<ItemStack> _retval = new AtomicReference<>(ItemStack.EMPTY);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).copy());
});
}
return _retval.get();
}
}.getItemStack(world, new BlockPos((int) x, (int) y, (int) z), 0))) + 1;
{
BlockEntity _ent = world.getBlockEntity(new BlockPos((int) x, (int) y, (int) z));
if (_ent != null) {
final int _sltid = 0;
final ItemStack _setstack = (new Object() {
public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int sltid) {
AtomicReference<ItemStack> _retval = new AtomicReference<>(ItemStack.EMPTY);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).copy());
});
}
return _retval.get();
}
}.getItemStack(world, new BlockPos((int) x, (int) y, (int) z), 0));
_setstack.setCount(1);
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
if (capability instanceof IItemHandlerModifiable) {
((IItemHandlerModifiable) capability).setStackInSlot(_sltid, _setstack);
}
});
}
}
((new Object() {
public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int sltid) {
AtomicReference<ItemStack> _retval = new AtomicReference<>(ItemStack.EMPTY);
BlockEntity _ent = world.getBlockEntity(pos);
if (_ent != null) {
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
_retval.set(capability.getStackInSlot(sltid).copy());
});
}
return _retval.get();
}
}.getItemStack(world, new BlockPos((int) x, (int) y, (int) z), 0))).enchant(OtherWorldsModModEnchantments.REAPING, (int) ench);
{
BlockEntity _ent = world.getBlockEntity(new BlockPos((int) x, (int) y, (int) z));
if (_ent != null) {
final int _sltid = 1;
final int _amount = 1;
_ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).ifPresent(capability -> {
if (capability instanceof IItemHandlerModifiable) {
ItemStack _stk = capability.getStackInSlot(_sltid).copy();
_stk.shrink(_amount);
((IItemHandlerModifiable) capability).setStackInSlot(_sltid, _stk);
}
});
}
}
}
The procedure has one of these "if"'s for every enchantment in the game. There is an augmented version used elsewhere in the procedure, but it has the same error: the sound plays, the item is removed from slot 1, but the enchantment level of the item in slot 0 never changes. This is very annoying, so any help is appreciated.
Procedure #2:
public static void execute(LevelAccessor world, double x, double y, double z) {
for (Direction directioniterator : Direction.values()) {
if (world instanceof Level _level)
_level.updateNeighborsAt(
new BlockPos((int) (Math.floor(x) + directioniterator.getStepX()), (int) (Math.floor(y) + directioniterator.getStepY()),
(int) (Math.floor(z) + directioniterator.getStepZ())),
_level.getBlockState(new BlockPos((int) (Math.floor(x) + directioniterator.getStepX()),
(int) (Math.floor(y) + directioniterator.getStepY()), (int) (Math.floor(z) + directioniterator.getStepZ())))
.getBlock());
}
}
This procedure is called for my redstone-emitting blocks "on update tick", which I assume means every tick. The procedure only seems to run when the block is placed, though, so I don't know if "on update tick" runs every tick. Does it? What should I do with it?
Both of these snippets are taken from part of the generated code, so parenthetical errors are either copy-paste errors or bugs in MCreator (the latter of which I doubt).
Thanks for any consideration.
I fixed procedure #2 (procedure #1 is still annoying). The problem was actually elsewhere:
This procedure always returns 15, which is obnoxious. Why is this? Is it a noob error that I overlooked?
Procedure #3:
This doesn't spawn any items. The procedure says that "entity dies" is a cancelable trigger, so I don't know what's wrong. Any help is appreciated.
Bumping because I haven't solved the problem.