Skip to content

Instantly share code, notes, and snippets.

@Commoble
Commoble / Intro.md
Last active February 3, 2021 22:26
Memory usage for blocks with lots of blockstates in minecraft forge

Let's say we have a block with lots of blockstates. How much memory does it use?

We can use Java Object Layout to get an estimate of how many bytes an instance of an object uses as described here: https://www.baeldung.com/jvm-measuring-object-sizes

So let's say we create a bunch of blocks with different blockstate properties. Java Object Layout has a graph-walking tool that will find every object reachable from a given instance, but most of these objects we find will be shared by other blocks.

However, Java Object Layout does let us get a graph layout for two objects and subtract the references in one from the other,

@Commoble
Commoble / NeighborNoticingBlock.java
Created December 28, 2020 20:51
Minecraft Forge 1.16.4: onBlockPlacedBy, updatePostPlacement, neighborChanged, onNeighborChange and observedNeighborChange
package commoble.gists;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
@Commoble
Commoble / DynamicDimensionHelper.java
Last active April 6, 2024 12:16
Dynamic Dimensions and How to Go to Dimensions in Minecraft Forge 1.16.4
package commoble.hyperbox;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Lifecycle;
@Commoble
Commoble / gist:1c52d985a714ea304c70a9885882f516
Created December 13, 2020 18:40
Minecraft Forge on-right-click-block logic flow
RIGHT CLICK BLOCK LOGIC FLOW -- SERVER THREAD (after interaction packet is received by server)
fire RightClickBlock event
if event is canceled, return the cancellation's action result type
if gametype is SPECTATOR, attempt to open the ContainerBlock container of the clicked block
return SUCCESS if block had a container, PASS otherwise
if event.getUseItem() is NOT DENIED, call ItemStack::onItemUseFirst (which usually delegates to Item::onItemUseFirst)
if the result of this is not PASS, return that result
@Commoble
Commoble / waterSlowdown.txt
Created October 10, 2020 20:13
Water Slowdown Mechanics in Minecraft Forge 1.16.3
water slowdown mechanics
assume an entity is in water
let moveSpeed = movement speed attribute
defaults to 0.7 for players?
let swimSpeed = swim speed modifier attribute
defaults to 1.0
let depthStriderEnchantment be 0F, 1F, 2F, or 3F (depends on enchantment tier, hard capped at 3) if on ground, or half that if treading water
let depthStriderFactor = depthStriderEnchantment / 3F (results in a number in the range [0,1])
@Commoble
Commoble / IdleTimeAndMobDespawningInMinecraftForge.txt
Created October 9, 2020 23:19
Idle Time and Mob Despawning in Minecraft Forge
idletime has these interactions:
is reset to 0 when:
when attacked
in the despawn check, when:
when the despawn fails due to persistance flag set or preventDespawn returning true
when the forge event is denied
when the nearest player is closer than the random despawn distance
when piglin or raider uses a ranged attack
when raider starts targeting a home
@Commoble
Commoble / CodecJsonDataManager.java
Last active October 3, 2020 18:28
Codec-based Json Data Loader for Minecraft Forge 1.16.3
/*
The MIT License (MIT)
Copyright (c) 2020 Joseph Bettendorff a.k.a. "Commoble"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@Commoble
Commoble / build.gradle
Last active January 15, 2024 06:05
How2ShadowJar in a minecraft forge mod's buildscript
// thanks to gigaherz for pointing me in the right directions on the buildscript
// The shadow gradle plugin assists with repackaging a 3rd-party library jar within your own jar
// In addition to ensuring that your end-users have the library available when they use your own thing,
// it also helps avoid collisions with other things that are also using the same library.
// As always, make sure the license of the library allows redistribution and is compatible with
// your own thing's license before redistributing it in this manner
buildscript {
@Commoble
Commoble / Fluids.java
Created August 29, 2020 04:03
Flowing Fluid Registry Helper for Minecraft Forge 1.16
public class FluidTest
{
public static final DeferredRegister<Fluid> FLUIDS = DeferredRegister.create(ForgeRegistries.FLUIDS, "modid");
public static final Pair<RegistryObject<ForgeFlowingFluid.Source>, RegistryObject<ForgeFlowingFluid.Flowing>> TEST_FLUIDS =
registerFluid("test_fluid", "flowing_test_fluid",
ForgeFlowingFluid.Source::new,
ForgeFlowingFluid.Flowing::new,
FluidAttributes.builder(new ResourceLocation("modid:block/test_fluid_still"), new ResourceLocation("modid:block/test_fluid_flowing")));
@Commoble
Commoble / ForgeModSkeleton.java
Last active December 24, 2020 18:18
Forge Mod Skeleton
@Mod(YourMod.MODID) // tells forge to construct this during modloading
public class YourMod
{
public static final String MODID = "yourmod"; // use this same string everywhere you need a modid
// Deferred Registers are forge's latest abstraction layer over the registries
// you give them suppliers and they create and register your things when the registry event happens
// don't forget to subscribe them to the mod bus in your mod constructor! this is important for making them work
// in 1.14 or old forge builds of 1.15 use `new DeferredRegister` instead of .create
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, YourMod.MODID);