Jump to content

[1.16.5] Problem with getting capabilities to work


TheDerpyParagon

Recommended Posts

I am making a mod that is based on the titan shifters from attack on titan, and I am trying to get capabilities to work in my mod, but my main class is failing me. (I think) My mod runs perfectly fine until I try to join a world, where it crashes with the error:

AL lib: (EE) alc_cleanup: 1 device not closed

And here is my main class:

package com.derpyzombie.titanshiftersmod;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.derpyzombie.titanshiftersmod.capabilities.ITitanShifters;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShifters;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersProvider;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersStorage;
import com.derpyzombie.titanshiftersmod.common.events.PlayerSizeControl;
import com.derpyzombie.titanshiftersmod.core.init.ItemInit;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("titanshiftersmod")
@Mod.EventBusSubscriber(modid = TitanShiftersMod.MOD_ID, bus = Bus.FORGE)
public class TitanShiftersMod
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID = "titanshiftersmod";
    public static final String MOD_VERSION = "0.1";

    public TitanShiftersMod() {
    	IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
    	
    	ItemInit.ITEMS.register(bus);
    	MinecraftForge.EVENT_BUS.register(new PlayerSizeControl());
    
    	MinecraftForge.EVENT_BUS.register(this);
    	
    	FMLJavaModLoadingContext.get().getModEventBus().addListener(TitanShiftersMod::onCommonSetup);
    }
    
    @SubscribeEvent
    public static void onCommonSetup(FMLCommonSetupEvent event) {
    	CapabilityManager.INSTANCE.register(ITitanShifters.class, new TitanShiftersStorage(), TitanShifters::new);
    }
    
    @SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) {
    	if (event.getObject() instanceof PlayerEntity) {
    		event.addCapability(new ResourceLocation(MOD_ID, "titanshifters"), new TitanShiftersProvider());
    	}
    }
    
//    @SubscribeEvent
//    public void playerLogsIn(PlayerLoggedInEvent event) {
//    	PlayerEntity player = event.getPlayer();
//    	LazyOptional<ITitanShifters> titanshifter = player.getCapability(TitanShiftersProvider.TITAN_SHIFTERS_CAPABILITY);
//		ITitanShifters titan = titanshifter.orElse(new TitanShifters());
//		player.sendMessage(new StringTextComponent("Hello from DerpyZombie!"), new UUID(0, 0));
//    }
}

When I made the onCommonSetup event and the AttachCapabilities event and subscribed to them, my game started to crash when I joined a world. It could be something to do with any of my other capability classes, I think, so I will also provide those.

ITitanShifters:

package com.derpyzombie.titanshiftersmod.capabilities;

public interface ITitanShifters {

	Boolean getPureTitan();
	Boolean getTitanShifter();
	Boolean getFoundingTitan();
	Boolean getAttackTitan();
	Boolean getColossalTitan();
	Boolean getJawTitan();
	Boolean getWarHammerTitan();
	Boolean getArmoredTitan();
	Boolean getBeastTitan();
	Boolean getFemaleTitan();
	Boolean getCartTitan();
	
	void setPureTitan(Boolean PureTitan);
	void setTitanShifter(Boolean TitanShifter);
	void setFoundingTitan(Boolean FoundingTitan);
	void setAttackTitan(Boolean AttackTitan);
	void setColossalTitan(Boolean ColossalTitan);
	void setJawTitan(Boolean JawTitan);
	void setWarHammerTitan(Boolean WarHammerTitan);
	void setArmoredTitan(Boolean ArmoredTitan);
	void setBeastTitan(Boolean BeastTitan);
	void setFemaleTitan(Boolean FemaleTitan);
	void setCartTitan(Boolean CartTitan);
}

TitanShifters:

package com.derpyzombie.titanshiftersmod.capabilities;

public class TitanShifters implements ITitanShifters {

	private Boolean PureTitan;
	private Boolean TitanShifter;
	private Boolean FoundingTitan;
	private Boolean AttackTitan;
	private Boolean ColossalTitan;
	private Boolean JawTitan;
	private Boolean WarHammerTitan;
	private Boolean ArmoredTitan;
	private Boolean BeastTitan;
	private Boolean FemaleTitan;
	private Boolean CartTitan;
	
	public TitanShifters() {
		this.TitanShifter = true;
		this.FoundingTitan = false;
		this.AttackTitan = false;
		this.ColossalTitan = false;
		this.JawTitan = false;
		this.WarHammerTitan = false;
		this.ArmoredTitan = false;
		this.BeastTitan = false;
		this.FemaleTitan = false;
		this.CartTitan = false;
	}

	@Override
	public Boolean getPureTitan() {
		return this.PureTitan;
	}
	
	@Override
	public Boolean getTitanShifter() {
		return this.TitanShifter;
	}

	@Override
	public Boolean getFoundingTitan() {
		return this.FoundingTitan;
	}

	@Override
	public Boolean getAttackTitan() {
		return this.AttackTitan;
	}

	@Override
	public Boolean getColossalTitan() {
		return this.ColossalTitan;
	}

	@Override
	public Boolean getJawTitan() {
		return this.JawTitan;
	}

	@Override
	public Boolean getWarHammerTitan() {
		return this.WarHammerTitan;
	}

	@Override
	public Boolean getArmoredTitan() {
		return this.ArmoredTitan;
	}

	@Override
	public Boolean getBeastTitan() {
		return this.BeastTitan;
	}

	@Override
	public Boolean getFemaleTitan() {
		return this.FemaleTitan;
	}

	@Override
	public Boolean getCartTitan() {
		return this.CartTitan;
	}

	@Override
	public void setPureTitan(Boolean PureTitan) {
		this.PureTitan = PureTitan;
	}
	
	@Override
	public void setTitanShifter(Boolean TitanShifter) {
		this.TitanShifter = TitanShifter;
	}

	@Override
	public void setFoundingTitan(Boolean FoundingTitan) {
		this.FoundingTitan = FoundingTitan;
	}

	@Override
	public void setAttackTitan(Boolean AttackTitan) {
		this.AttackTitan = AttackTitan;
	}

	@Override
	public void setColossalTitan(Boolean ColossalTitan) {
		this.ColossalTitan = ColossalTitan;
	}

	@Override
	public void setJawTitan(Boolean JawTitan) {
		this.JawTitan = JawTitan;
	}

	@Override
	public void setWarHammerTitan(Boolean WarHammerTitan) {
		this.WarHammerTitan = WarHammerTitan;
	}

	@Override
	public void setArmoredTitan(Boolean ArmoredTitan) {
		this.ArmoredTitan = ArmoredTitan;
	}

	@Override
	public void setBeastTitan(Boolean BeastTitan) {
		this.BeastTitan = BeastTitan;
	}

	@Override
	public void setFemaleTitan(Boolean FemaleTitan) {
		this.FemaleTitan = FemaleTitan;
	}

	@Override
	public void setCartTitan(Boolean CartTitan) {
		this.CartTitan = CartTitan;
	}
}

TitanShiftersProvider:

package com.derpyzombie.titanshiftersmod.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

public class TitanShiftersProvider implements ICapabilitySerializable<INBT> {

	@CapabilityInject(ITitanShifters.class)
	public static final Capability<ITitanShifters> TITAN_SHIFTERS_CAPABILITY = null;

	private LazyOptional<ITitanShifters> instance = LazyOptional.of(TITAN_SHIFTERS_CAPABILITY::getDefaultInstance);
	
	public Boolean hasCapability(Capability<?> capability, Direction side) {
		return capability == TITAN_SHIFTERS_CAPABILITY;
	}
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
		return cap == TITAN_SHIFTERS_CAPABILITY ? instance.cast() : LazyOptional.empty();
	}

	@Override
	public INBT serializeNBT() {
		//@formatter:off
		return TITAN_SHIFTERS_CAPABILITY.getStorage().writeNBT(TITAN_SHIFTERS_CAPABILITY,
				instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null);
		//@formatter:on
	}

	@Override
	public void deserializeNBT(INBT nbt) {
		//@formatter:off
		TITAN_SHIFTERS_CAPABILITY.getStorage().readNBT(TITAN_SHIFTERS_CAPABILITY,
				instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt);
		//@formatter:on
	}

}

TitanShiftersStorage:

package com.derpyzombie.titanshiftersmod.capabilities;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;

public class TitanShiftersStorage implements IStorage<ITitanShifters> {

	@Override
	public INBT writeNBT(Capability<ITitanShifters> capability, ITitanShifters instance, Direction side) {
		CompoundNBT tag = new CompoundNBT();
		tag.putBoolean("TitanShifter", instance.getTitanShifter());
		tag.putBoolean("FoundingTitan", instance.getFoundingTitan());
		tag.putBoolean("AttackTitan", instance.getAttackTitan());
		tag.putBoolean("ColossalTitan", instance.getColossalTitan());
		tag.putBoolean("JawTitan", instance.getJawTitan());
		tag.putBoolean("WarHammerTitan", instance.getWarHammerTitan());
		tag.putBoolean("ArmoredTitan", instance.getArmoredTitan());
		tag.putBoolean("BeastTitan", instance.getBeastTitan());
		tag.putBoolean("FemaleTitan", instance.getFemaleTitan());
		tag.putBoolean("CartTitan", instance.getCartTitan());
		tag.putBoolean("PureTitan", instance.getPureTitan());
		return tag;
	}

	@Override
	public void readNBT(Capability<ITitanShifters> capability, ITitanShifters instance, Direction side, INBT nbt) {
		CompoundNBT tag = (CompoundNBT) nbt;
		instance.setTitanShifter(tag.getBoolean("TitanShifter"));
		instance.setFoundingTitan(tag.getBoolean("FoundingTitan"));
		instance.setAttackTitan(tag.getBoolean("AttackTitan"));
		instance.setColossalTitan(tag.getBoolean("ColossalTitan"));
		instance.setJawTitan(tag.getBoolean("JawTitan"));
		instance.setWarHammerTitan(tag.getBoolean("WarHammerTitan"));
		instance.setArmoredTitan(tag.getBoolean("ArmoredTitan"));
		instance.setBeastTitan(tag.getBoolean("BeastTitan"));
		instance.setFemaleTitan(tag.getBoolean("FemaleTitan"));
		instance.setCartTitan(tag.getBoolean("CartTitan"));
		instance.setPureTitan(tag.getBoolean("PureTitan"));
	}

}

 

Link to comment
Share on other sites

Whoops, my bad.

Here's the error:

[15:57:57] [Netty Local Client IO #0/INFO] [ne.mi.fm.ne.NetworkHooks/]: Connected to a modded server.
[15:57:57] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: Duplicate Capability Key: titanshiftersmod:titanshifters com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersProvider@471e4c80
	Index: 2
	Listeners:
		0: NORMAL
		1: ASM: class com.derpyzombie.titanshiftersmod.capabilities.CapabilityAttacher onAttachCapabilities(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V
		2: ASM: class com.derpyzombie.titanshiftersmod.TitanShiftersMod onAttachCapabilities(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V
java.lang.IllegalStateException: Duplicate Capability Key: titanshiftersmod:titanshifters com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersProvider@471e4c80
	at net.minecraftforge.event.AttachCapabilitiesEvent.addCapability(AttachCapabilitiesEvent.java:71)
	at com.derpyzombie.titanshiftersmod.TitanShiftersMod.onAttachCapabilities(TitanShiftersMod.java:55)
	at net.minecraftforge.eventbus.ASMEventHandler_10_TitanShiftersMod_onAttachCapabilities_AttachCapabilitiesEvent.invoke(.dynamic)
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:597)
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:591)
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:48)
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:44)
	at net.minecraft.entity.Entity.<init>(Entity.java:221)
	at net.minecraft.entity.LivingEntity.<init>(LivingEntity.java:205)
	at net.minecraft.entity.player.PlayerEntity.<init>(PlayerEntity.java:159)
	at net.minecraft.entity.player.ServerPlayerEntity.<init>(ServerPlayerEntity.java:179)
	at net.minecraft.server.management.PlayerList.getPlayerForLogin(PlayerList.java:406)
	at net.minecraft.network.login.ServerLoginNetHandler.handleAcceptedLogin(ServerLoginNetHandler.java:118)
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:65)
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:222)
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:134)
	at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:865)
	at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:787)
	at net.minecraft.server.integrated.IntegratedServer.tickServer(IntegratedServer.java:78)
	at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:642)
	at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:232)
	at java.lang.Thread.run(Unknown Source)

 

Link to comment
Share on other sites

Ah, thanks.

the game throws another error at me after this, though.

[16:25:54] [Server thread/INFO] [minecraft/MinecraftServer]: Dev joined the game
[16:25:55] [Render thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: null
	Index: 1
	Listeners:
		0: NORMAL
		1: ASM: class com.derpyzombie.titanshiftersmod.common.events.PlayerSizeControl crouching(Lnet/minecraftforge/event/entity/player/PlayerEvent;)V
java.lang.NullPointerException
	at com.derpyzombie.titanshiftersmod.common.events.PlayerSizeControl.crouching(PlayerSizeControl.java:51)
	at net.minecraftforge.eventbus.ASMEventHandler_6_PlayerSizeControl_crouching_PlayerEvent.invoke(.dynamic)
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
	at net.minecraftforge.event.ForgeEventFactory.onItemTooltip(ForgeEventFactory.java:289)
	at net.minecraft.item.ItemStack.getTooltipLines(ItemStack.java:702)
	at net.minecraft.client.Minecraft.lambda$null$11(Minecraft.java:665)
	at java.util.stream.ReferencePipeline$7$1.accept(Unknown Source)
	at java.util.Collections$2.tryAdvance(Unknown Source)
	at java.util.Collections$2.forEachRemaining(Unknown Source)
	at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
	at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
	at java.util.stream.ReferencePipeline.forEach(Unknown Source)
	at net.minecraft.client.util.SearchTree.index(SearchTree.java:35)
	at net.minecraft.client.util.SearchTreeReloadable.add(SearchTreeReloadable.java:46)
	at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:408)
	at net.minecraft.client.network.play.ClientPlayNetHandler.handleUpdateRecipes(ClientPlayNetHandler.java:1312)
	at net.minecraft.network.play.server.SUpdateRecipesPacket.handle(SUpdateRecipesPacket.java:27)
	at net.minecraft.network.play.server.SUpdateRecipesPacket.handle(SUpdateRecipesPacket.java:16)
	at net.minecraft.network.PacketThreadUtil.lambda$ensureRunningOnSameThread$0(PacketThreadUtil.java:19)
	at net.minecraft.util.concurrent.ThreadTaskExecutor.doRunTask(ThreadTaskExecutor.java:136)
	at net.minecraft.util.concurrent.RecursiveEventLoop.doRunTask(RecursiveEventLoop.java:22)
	at net.minecraft.util.concurrent.ThreadTaskExecutor.pollTask(ThreadTaskExecutor.java:109)
	at net.minecraft.util.concurrent.ThreadTaskExecutor.runAllTasks(ThreadTaskExecutor.java:97)
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:947)
	at net.minecraft.client.Minecraft.run(Minecraft.java:607)
	at net.minecraft.client.main.Main.main(Main.java:184)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52)
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37)
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54)
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72)
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82)
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66)
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:108)

This error seems really weird to me because when I get this error, I am loading into the world and I kind of spawn in. (I can see the middle cursor but the world around me hasn't loaded yet.) However, other times I can load in perfectly fine but the game crashes after a few seconds of playing. About 15 seconds into playing. Here's the modified main class just in case someone wants it:

package com.derpyzombie.titanshiftersmod;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.derpyzombie.titanshiftersmod.capabilities.ITitanShifters;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShifters;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersProvider;
import com.derpyzombie.titanshiftersmod.capabilities.TitanShiftersStorage;
import com.derpyzombie.titanshiftersmod.common.events.PlayerSizeControl;
import com.derpyzombie.titanshiftersmod.core.init.ItemInit;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("titanshiftersmod")
@Mod.EventBusSubscriber(modid = TitanShiftersMod.MOD_ID, bus = Bus.FORGE)
public class TitanShiftersMod
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID = "titanshiftersmod";
    public static final String MOD_VERSION = "0.1";
    
    public static final ResourceLocation resourceLocation = new ResourceLocation(MOD_ID, "puretitan");

    public TitanShiftersMod() {
    	IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
    	
    	ItemInit.ITEMS.register(bus);
    	MinecraftForge.EVENT_BUS.register(new PlayerSizeControl());
    
    	MinecraftForge.EVENT_BUS.register(this);
    	
    	bus.addListener(TitanShiftersMod::onCommonSetup);
    }
    
    @SubscribeEvent
    public static void onCommonSetup(FMLCommonSetupEvent event) {
    	CapabilityManager.INSTANCE.register(ITitanShifters.class, new TitanShiftersStorage(), TitanShifters::new);
    }
    
     @SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) {
    	if (event.getObject() instanceof PlayerEntity) {
    		event.addCapability(resourceLocation, new TitanShiftersProvider());
    	}
    }
    
//    @SubscribeEvent
//    public void playerLogsIn(PlayerLoggedInEvent event) {
//    	PlayerEntity player = event.getPlayer();
//    	LazyOptional<ITitanShifters> titanshifter = player.getCapability(TitanShiftersProvider.TITAN_SHIFTERS_CAPABILITY);
//		ITitanShifters titan = titanshifter.orElse(new TitanShifters());
//		player.sendMessage(new StringTextComponent("Hello from DerpyZombie!"), new UUID(0, 0));
//    }
}

 

Link to comment
Share on other sites

age com.derpyzombie.titanshiftersmod.common.events;

import com.derpyzombie.titanshiftersmod.TitanShiftersMod;
import com.derpyzombie.titanshiftersmod.common.items.TitanSpinalFluid;

import net.minecraft.entity.Pose;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@EventBusSubscriber(modid = TitanShiftersMod.MOD_ID, bus = Bus.FORGE)
public class PlayerSizeControl {
	
    private static float sizemultiplier = 1f;
    private static float stepUp = 0.5f;
   
    public static void changeSize(float size) {
    		sizemultiplier = size;
    }
    
    @SubscribeEvent
    public void onPlayerRenderPre(RenderPlayerEvent.Pre event){
    	
        event.getMatrixStack().pushPose();
        event.getMatrixStack().scale(sizemultiplier, sizemultiplier, sizemultiplier);
    }

    @SubscribeEvent
    public void onPlayerRenderPost(RenderPlayerEvent.Post event){
    	
        	event.getMatrixStack().popPose();
    }
    
    @SubscribeEvent
    public static void changeHeight(EntityEvent.Size event) {
    	
    	if (event.getEntity() instanceof PlayerEntity) {
    		
    			event.setNewEyeHeight(sizemultiplier * 1.6f);
    			event.setNewSize(event.getNewSize().scale(sizemultiplier));
    			event.getEntity().maxUpStep = stepUp;
    	}
    }
    
    @SubscribeEvent
    public static void crouching(PlayerEvent player) {
    		player.getEntity().setPose(Pose.CROUCHING); **********
    		player.getEntity().setPose(Pose.STANDING);
    }
}

The line with the stars next to it is line 51.

I see I have made a mistake here, because this event will basically constantly crouch then stand back up. This event is only supposed to happen when a certain condition is met. I'm not sure why this would crash the game, though.

Link to comment
Share on other sites

On 11/25/2021 at 1:33 AM, TheDerpyParagon said:
java.lang.NullPointerException
	at com.derpyzombie.titanshiftersmod.common.events.PlayerSizeControl.crouching(PlayerSizeControl.java:51)

this is the error, but your code looks okay, not sure why it will throw there a NullPointerException, can you post a git repo

Link to comment
Share on other sites

Even though I have quite limited experience, why did you split your item registry into a package called "core"? that is confusing. It should be located where the rest of the item classes are, in common. Also, your PlayerSize class is in common. But the graphics transformations are client-sided. Let me explain the structure I use:

Common package, for anything that is needed on both the client and server
	items
		Item classes
		Item registry class
	blocks
		Block classes
		Block registry class
	network
		Packet classes
		Network manager class
    	

Client package, for anything that can only run on the client
	gui
		overlay
			Overlay classes
			Overlay renderer
	
and so on.

You need to send a packet to other clients (tracking entities) when doing the transformation. See the forge documentation on Simple Channel.

Link to comment
Share on other sites

Also, your resourcelocations are broken. You cannot use capital letters in resource locations. For example, your 

LjaWAr8.png

caused forge to not load the mod. Replace it with puretitan in all occurences. 

Your NBT is failing to save because you did not assign a default value to the PureTitan boolean in the constructor, so the boolean is null. Coming from a C# background, I actually blame java for allowing booleans to be nullable. 

Link to comment
Share on other sites

5 hours ago, matthew123 said:

Even though I have quite limited experience, why did you split your item registry into a package called "core"? that is confusing. It should be located where the rest of the item classes are, in common. Also, your PlayerSize class is in common. But the graphics transformations are client-sided. Let me explain the structure I use:

it doesn't matter at all, it is his decision how he structures his project (Edit: there is no predefined package structure from Forge, however, it is recommended to use a package structure similar to minecraft or forge)

5 hours ago, matthew123 said:

Also, your repo's structure is messed up. You missed the gradle files. 

in addition to matthew123, use a Git Client to upload your project

5 hours ago, matthew123 said:

Your NBT is failing to save because you did not assign a default value to the PureTitan boolean in the constructor, so the boolean is null. Coming from a C# background, I actually blame java for allowing booleans to be nullable. 

the reason for this is that you usually use primitive data types, like boolean, int, double and not their classes (Boolean, Integer, Double)

5 hours ago, matthew123 said:

the mod looks like it's working now. But do remember to fix the structure, and use packets for synchronization.

update your git repo and tell us how the reproduce the error

Edited by Luis_ST
Link to comment
Share on other sites

21 hours ago, matthew123 said:

Even though I have quite limited experience, why did you split your item registry into a package called "core"? that is confusing. It should be located where the rest of the item classes are, in common. Also, your PlayerSize class is in common. But the graphics transformations are client-sided. Let me explain the structure I use:

Common package, for anything that is needed on both the client and server
	items
		Item classes
		Item registry class
	blocks
		Block classes
		Block registry class
	network
		Packet classes
		Network manager class
    	

Client package, for anything that can only run on the client
	gui
		overlay
			Overlay classes
			Overlay renderer
	
and so on.

You need to send a packet to other clients (tracking entities) when doing the transformation. See the forge documentation on Simple Channel.

The reason I have structured my mod like this is because when I first got into modding, which wasn't too long ago, I watched a tutorial to get started, and the person in the tutorial structured their mod like that, so that's just what I've been doing. What you said does make more sense to me, though, so I'll change up my structure to make more sense. As for the client package, I'm going to keep my PlayerSizeControl class in common because yes, there are client-sided things in the class like the renderPlayerEvent events, but there are other things that are non-client-sided like EntityEvent.size. I'm pretty sure client-sided means just rendering, correct?

 

21 hours ago, matthew123 said:

Also, your resourcelocations are broken. You cannot use capital letters in resource locations. For example, your 

LjaWAr8.png

caused forge to not load the mod. Replace it with puretitan in all occurences. 

Your NBT is failing to save because you did not assign a default value to the PureTitan boolean in the constructor, so the boolean is null. Coming from a C# background, I actually blame java for allowing booleans to be nullable. 

Thanks, it would've taken me a long time to notice that I didn't assign any value to the pureTitan Boolean in my constructor. I'll also change my resource location to have no capitals.

 

Link to comment
Share on other sites

2 hours ago, TheDerpyParagon said:

The reason I have structured my mod like this is because when I first got into modding, which wasn't too long ago, I watched a tutorial to get started, and the person in the tutorial structured their mod like that, so that's just what I've been doing. What you said does make more sense to me, though, so I'll change up my structure to make more sense. As for the client package, I'm going to keep my PlayerSizeControl class in common because yes, there are client-sided things in the class like the renderPlayerEvent events, but there are other things that are non-client-sided like EntityEvent.size. I'm pretty sure client-sided means just rendering, correct?

 

Thanks, it would've taken me a long time to notice that I didn't assign any value to the pureTitan Boolean in my constructor. I'll also change my resource location to have no capitals.

 

You should change your types to "boolean", so you can't get problems like these in the future.

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.

Announcements



×
×
  • Create New...

Important Information

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