Problem with multiple procedures

Started by IAskModdingQuestions on Sun, 05/08/2022 - 00:44

Topic category: Help with modding (Java Edition)

Last seen on 22:34, 21. May 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Problem with multiple procedures
Sun, 05/08/2022 - 00:44

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.

Last seen on 22:34, 21. May 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I fixed procedure #2 …
Tue, 05/10/2022 - 22:08

I fixed procedure #2 (procedure #1 is still annoying). The problem was actually elsewhere:

public static double execute(LevelAccessor world, double x, double y, double z) {
		double redstone = 0;
		redstone = 0;
		if (((new Object() {
			public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int slotid) {
				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(slotid).copy()));
				return _retval.get();
			}
		}.getItemStack(world, new BlockPos(x, y, z), 0)).getItem() instanceof BlockItem _bi
				? _bi.getBlock().defaultBlockState()
				: Blocks.AIR.defaultBlockState()).getBlock() == (world.getBlockState(new BlockPos(x + (new Object() {
					public Direction getDirection(BlockPos pos) {
						BlockState _bs = world.getBlockState(pos);
						Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
						if (property != null && _bs.getValue(property) instanceof Direction _dir)
							return _dir;
						property = _bs.getBlock().getStateDefinition().getProperty("axis");
						if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
							return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
						return Direction.NORTH;
					}
				}.getDirection(new BlockPos(x, y, z))).getStepX() * 2, y + (new Object() {
					public Direction getDirection(BlockPos pos) {
						BlockState _bs = world.getBlockState(pos);
						Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
						if (property != null && _bs.getValue(property) instanceof Direction _dir)
							return _dir;
						property = _bs.getBlock().getStateDefinition().getProperty("axis");
						if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
							return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
						return Direction.NORTH;
					}
				}.getDirection(new BlockPos(x, y, z))).getStepY() * 2, z + (new Object() {
					public Direction getDirection(BlockPos pos) {
						BlockState _bs = world.getBlockState(pos);
						Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
						if (property != null && _bs.getValue(property) instanceof Direction _dir)
							return _dir;
						property = _bs.getBlock().getStateDefinition().getProperty("axis");
						if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
							return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
						return Direction.NORTH;
					}
				}.getDirection(new BlockPos(x, y, z))).getStepZ() * 2))).getBlock() || ((new Object() {
					public ItemStack getItemStack(LevelAccessor world, BlockPos pos, int slotid) {
						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(slotid).copy()));
						return _retval.get();
					}
				}.getItemStack(world, new BlockPos(x, y, z), 0)).getItem() instanceof BucketItem _bucket
						? _bucket.getFluid().defaultFluidState().createLegacyBlock()
						: Blocks.AIR.defaultBlockState()).getBlock() == (world.getBlockState(new BlockPos(x + (new Object() {
							public Direction getDirection(BlockPos pos) {
								BlockState _bs = world.getBlockState(pos);
								Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
								if (property != null && _bs.getValue(property) instanceof Direction _dir)
									return _dir;
								property = _bs.getBlock().getStateDefinition().getProperty("axis");
								if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
									return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
								return Direction.NORTH;
							}
						}.getDirection(new BlockPos(x, y, z))).getStepX() * 2, y + (new Object() {
							public Direction getDirection(BlockPos pos) {
								BlockState _bs = world.getBlockState(pos);
								Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
								if (property != null && _bs.getValue(property) instanceof Direction _dir)
									return _dir;
								property = _bs.getBlock().getStateDefinition().getProperty("axis");
								if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
									return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
								return Direction.NORTH;
							}
						}.getDirection(new BlockPos(x, y, z))).getStepY() * 2, z + (new Object() {
							public Direction getDirection(BlockPos pos) {
								BlockState _bs = world.getBlockState(pos);
								Property<?> property = _bs.getBlock().getStateDefinition().getProperty("facing");
								if (property != null && _bs.getValue(property) instanceof Direction _dir)
									return _dir;
								property = _bs.getBlock().getStateDefinition().getProperty("axis");
								if (property != null && _bs.getValue(property) instanceof Direction.Axis _axis)
									return Direction.fromAxisAndDirection(_axis, Direction.AxisDirection.POSITIVE);
								return Direction.NORTH;
							}
						}.getDirection(new BlockPos(x, y, z))).getStepZ() * 2))).getBlock()) {
			redstone = 15;
		}
		return redstone;
	}

This procedure always returns 15, which is obnoxious. Why is this? Is it a noob error that I overlooked?

Procedure #3:

@SubscribeEvent
	public static void onEntityDeath(LivingDeathEvent event) {
		if (event != null && event.getEntity() != null) {
			execute(event, event.getEntity().level, event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), event.getEntity(),
					event.getSource().getEntity());
		}
	}

	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity, Entity sourceentity) {
		execute(null, world, x, y, z, entity, sourceentity);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, Entity entity, Entity sourceentity) {
		if (entity == null || sourceentity == null)
			return;
		ItemStack inhand = ItemStack.EMPTY;
		double reapinglevel = 0;
		inhand = (sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY);
		reapinglevel = EnchantmentHelper.getItemEnchantmentLevel(OtherWorldsModModEnchantments.REAPING.get(), inhand);
		if (inhand.getItem() instanceof HoeItem && reapinglevel > 0
				&& !(entity instanceof LivingEntity _livEnt ? _livEnt.getMobType() == MobType.UNDEAD : false)) {
			if (event != null && event.isCancelable()) {
				event.setCanceled(true);
			}
			if (world instanceof Level _level && !_level.isClientSide()) {
				ItemEntity entityToSpawn = new ItemEntity(_level, x, y, z, new ItemStack(OtherWorldsModModItems.SOUL.get()));
				entityToSpawn.setPickUpDelay(10);
				_level.addFreshEntity(entityToSpawn);
			}
		}
	}

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.

Last seen on 22:34, 21. May 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Bumping because I haven't…
Wed, 05/11/2022 - 21:34

Bumping because I haven't solved the problem.