Jump to content

[1.20.1] - How do i make loops work, in a lighter type situation


modrinth

Recommended Posts

So I'm trying to create a lighter, so On and Off. Here is my code

package icelock.iceblocks.item.custom;

import com.mojang.brigadier.ParseResults;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ChatComponent;
import net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen;
import net.minecraft.client.sounds.SoundEngine;
import net.minecraft.client.telemetry.TelemetryProperty;
import net.minecraft.commands.*;
import net.minecraft.commands.arguments.GameModeArgument;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.commands.GameModeCommand;
import net.minecraft.server.commands.OpCommand;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerPlayerGameMode;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.server.*;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.storage.LevelResource;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.CustomizeGuiOverlayEvent;
import net.minecraftforge.common.ForgeConfig;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.server.console.TerminalHandler;
import net.minecraft.commands.CommandSourceStack;

import java.nio.file.Path;

public class FlashLight extends Item {
public FlashLight(Properties pProperties) {
super(pProperties);
}
boolean FlashSwitch = false;
Vec3 pos1 = null;
Vec3 pos2 = null;
Direction Dir = null;
boolean ON = false;

@Override
public InteractionResult useOn(UseOnContext pContext) {
if (!pContext.getLevel().isClientSide) {
Boolean ShiftDown = pContext.isSecondaryUseActive();
Player player = pContext.getPlayer();
MinecraftServer server = pContext.getPlayer().getServer();
if (FlashSwitch) {
pos1 = pContext.getPlayer().getEyePosition();
FlashSwitch = !FlashSwitch;
player.displayClientMessage(Component.literal("§cOff"),true);
ON = false;
} else if (!FlashSwitch) {
FlashSwitch = !FlashSwitch;
player.displayClientMessage(Component.literal("§eOn"),true);
ON = true;

while (ON) {
Dir = pContext.getPlayer().getDirection();
if (Dir.equals(Direction.NORTH)){
player.displayClientMessage(Component.literal("§eNORTH"),true);
} else if (Dir.equals(Direction.EAST)){
player.displayClientMessage(Component.literal("§eEAST"),true);
} else if (Dir.equals(Direction.SOUTH)){
player.displayClientMessage(Component.literal("§eSOUTH"),true);
} else if (Dir.equals(Direction.WEST)){
player.displayClientMessage(Component.literal("§eWEST"),true);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}


}
}
}
return InteractionResult.SUCCESS;
}
}
This barely works, the switch between on and off doesnt work, and when i turn directions it doesnt display the direction im facing, please help ASAP.

Link to comment
Share on other sites

11 minutes ago, modrinth said:

boolean FlashSwitch = false;

Blocks and items are singletons, this value is shared across all "instances" of your item. You have to store this data in an ItemStack's capability.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

That's not really the problem, i have a separate item with the exact same on and off switch and it works perfectly fine. The problem is something with the loop i presume, as without it the switch works perfectly fine. I'll explain what i need the item to do, first off blocks cant be transparent (or at-least that's what i think) so i thought of setting the block behind the player to a black block with a light level of like 6 or something. I need to detect the direction the player is facing at all times so i can set the block behind them to a light block.

Edited by modrinth
Link to comment
Share on other sites

43 minutes ago, modrinth said:

That's not really the problem, i have a separate item with the exact same on and off switch and it works perfectly fine. The problem is something with the loop i presume, as without it the switch works perfectly fine. I'll explain what i need the item to do, first off blocks cant be transparent (or at-least that's what i think) so i thought of setting the block behind the player to a black block with a light level of like 6 or something. I need to detect the direction the player is facing at all times so i can set the block behind them to a light block.

Nevermind i just figured out that i can make transparent blocks with AirBlock, that saves alot of time.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.