Skip to content

Commit

Permalink
Add sign support to wood object, plus add support for the registry ad…
Browse files Browse the repository at this point in the history
…apters

Also fixes slabs being registered with a too large burn time
  • Loading branch information
KnightMiner committed Jul 8, 2021
1 parent 983c211 commit 542851f
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 39 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ mod_version=1.6

# Minecraft Version Information
minecraft_version=1.16.5
minecraft_range=[1.16.2,1.17)
minecraft_range=[1.16.5,1.17)

# Forge Version Information
loader_range=[33.0,)
forge_version=36.0.0
forge_range=[33.0.61,)
forge_version=36.1.1
forge_range=[36.1.1,)

# Mappings Information
mappings_version=20200916-1.16.2
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/slimeknights/mantle/Mantle.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package slimeknights.mantle;

import com.google.common.collect.ImmutableSet;
import net.minecraft.block.Block;
import net.minecraft.data.DataGenerator;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.crafting.CraftingHelper;
Expand Down Expand Up @@ -32,6 +35,7 @@
import slimeknights.mantle.recipe.crafting.ShapedRetexturedRecipe;
import slimeknights.mantle.recipe.ingredient.IngredientIntersection;
import slimeknights.mantle.recipe.ingredient.IngredientWithout;
import slimeknights.mantle.registration.RegistrationHelper;
import slimeknights.mantle.registration.adapter.RegistryAdapter;
import slimeknights.mantle.util.OffhandCooldownTracker;

Expand All @@ -44,7 +48,6 @@
*/
@Mod(Mantle.modId)
public class Mantle {

public static final String modId = "mantle";
public static final Logger logger = LogManager.getLogger("Mantle");

Expand All @@ -71,6 +74,14 @@ private void commonSetup(final FMLCommonSetupEvent event) {
MantleCommand.init();
OffhandCooldownTracker.register();
MantleTags.init();

// inject our new signs into the tile entity type
event.enqueueWork(() -> {
ImmutableSet.Builder<Block> builder = ImmutableSet.builder();
builder.addAll(TileEntityType.SIGN.validBlocks);
RegistrationHelper.forEachSignBlock(builder::add);
TileEntityType.SIGN.validBlocks = builder.build();
});
}

private void gatherData(final GatherDataEvent event) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/slimeknights/mantle/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.GameSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.renderer.Atlases;
import net.minecraft.client.settings.AttackIndicatorStatus;
import net.minecraft.item.ItemStack;
import net.minecraft.resources.IReloadableResourceManager;
Expand Down Expand Up @@ -34,6 +35,7 @@
import slimeknights.mantle.client.model.inventory.InventoryModel;
import slimeknights.mantle.client.model.util.ModelHelper;
import slimeknights.mantle.data.MantleTags;
import slimeknights.mantle.registration.RegistrationHelper;
import slimeknights.mantle.util.OffhandCooldownTracker;

@SuppressWarnings("unused")
Expand All @@ -46,6 +48,7 @@ static void clientSetup(FMLClientSetupEvent event) {
if (manager instanceof IReloadableResourceManager) {
((IReloadableResourceManager)manager).addReloadListener(ModelHelper.LISTENER);
}
event.enqueueWork(() -> RegistrationHelper.forEachWoodType(Atlases::addWoodType));
}

@SubscribeEvent
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/slimeknights/mantle/item/BurnableSignItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package slimeknights.mantle.item;

import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SignItem;

public class BurnableSignItem extends SignItem {
private final int burnTime;
public BurnableSignItem(Properties propertiesIn, Block floorBlockIn, Block wallBlockIn, int burnTime) {
super(propertiesIn, floorBlockIn, wallBlockIn);
this.burnTime = burnTime;
}

@Override
public int getBurnTime(ItemStack itemStack) {
return burnTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.minecraft.block.Block;
import net.minecraft.block.WoodType;
import net.minecraftforge.event.RegistryEvent.MissingMappings;
import net.minecraftforge.event.RegistryEvent.MissingMappings.Mapping;
import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraftforge.registries.IRegistryDelegate;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RegistrationHelper {
/** Wood types to register with the texture atlas */
private static final List<WoodType> WOOD_TYPES = new ArrayList<>();
/** Sign blocks to inject into the sign tile entity */
private static final List<Supplier<? extends Block>> SIGN_BLOCKS = new ArrayList<>();

/**
* Used to mark injected registry objects, as despite being set to null they will be nonnull at runtime.
* @param <T> Class type
Expand Down Expand Up @@ -51,4 +61,31 @@ public static <T extends IForgeRegistryEntry<T>> void handleMissingMappings(Miss
}
}
}

/** Registers a wood type to be injected into the atlas, should be called before client setup */
public static void registerWoodType(WoodType type) {
synchronized (WOOD_TYPES) {
WOOD_TYPES.add(type);
}
}

/**
* Registers a sign block to be injected into the tile entity, should be called before common setup
* @param sign Sign block supplier
*/
public static void registerSignBlock(Supplier<? extends Block> sign) {
synchronized (SIGN_BLOCKS) {
SIGN_BLOCKS.add(sign);
}
}

/** Runs the given consumer for each wood type registered */
public static void forEachWoodType(Consumer<WoodType> consumer) {
WOOD_TYPES.forEach(consumer);
}

/** Runs the given consumer for each wood type registered */
public static void forEachSignBlock(Consumer<? super Block> consumer) {
SIGN_BLOCKS.forEach(block -> consumer.accept(block.get()));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package slimeknights.mantle.registration.adapter;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.AbstractBlock.Properties;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.FenceBlock;
import net.minecraft.block.FenceGateBlock;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.block.PressurePlateBlock;
import net.minecraft.block.PressurePlateBlock.Sensitivity;
import net.minecraft.block.RotatedPillarBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.SoundType;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.StandingSignBlock;
import net.minecraft.block.TrapDoorBlock;
import net.minecraft.block.WallBlock;
import net.minecraft.block.WallSignBlock;
import net.minecraft.block.WoodButtonBlock;
import net.minecraft.block.WoodType;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.Direction;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.block.StrippableLogBlock;
import slimeknights.mantle.block.WoodenDoorBlock;
import slimeknights.mantle.registration.RegistrationHelper;
import slimeknights.mantle.registration.object.BuildingBlockObject;
import slimeknights.mantle.registration.object.FenceBuildingBlockObject;
import slimeknights.mantle.registration.object.WallBuildingBlockObject;
import slimeknights.mantle.registration.object.WoodBlockObject;

import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -91,6 +113,56 @@ public FenceBuildingBlockObject registerFenceBuilding(Block block, String name)
);
}


/**
* Registers a new wood object
* @param name Name of the wood object
* @param planksMaterial Material for the planks
* @param planksColor Map color for the planks
* @param plankSound Sound for the planks
* @param planksTool Tool for the planks
* @param barkMaterial Bark material
* @param barkColor Map color for the bark
* @param barkSound Sound for the bark
* @param group Item group
* @return Wood object
*/
public WoodBlockObject registerWood(String name, Material planksMaterial, MaterialColor planksColor, SoundType plankSound, ToolType planksTool, Material barkMaterial, MaterialColor barkColor, SoundType barkSound, ItemGroup group) {
WoodType woodType = WoodType.create(resourceName(name));
RegistrationHelper.registerWoodType(woodType);
Item.Properties itemProps = new Item.Properties().group(group);

// planks
AbstractBlock.Properties planksProps = AbstractBlock.Properties.create(planksMaterial, planksColor).harvestTool(planksTool).hardnessAndResistance(2.0f, 3.0f).sound(plankSound);
BuildingBlockObject planks = registerBuilding(new Block(planksProps), name + "_planks");
FenceBlock fence = register(new FenceBlock(Properties.from(planks.get())), name + "_fence");
// logs and wood
Supplier<? extends RotatedPillarBlock> stripped = () -> new RotatedPillarBlock(AbstractBlock.Properties.create(planksMaterial, planksColor).harvestTool(planksTool).hardnessAndResistance(2.0f).sound(plankSound));
RotatedPillarBlock strippedLog = register(stripped.get(), "stripped_" + name + "_log");
RotatedPillarBlock strippedWood = register(stripped.get(), "stripped_" + name + "_wood");
RotatedPillarBlock log = register(new StrippableLogBlock(strippedLog.delegate, AbstractBlock.Properties.create(
barkMaterial, state -> state.get(RotatedPillarBlock.AXIS) == Direction.Axis.Y ? planksColor : barkColor)
.harvestTool(ToolType.AXE).hardnessAndResistance(2.0f).sound(barkSound)), name + "_log");
RotatedPillarBlock wood = register(new StrippableLogBlock(strippedWood.delegate, AbstractBlock.Properties.create(barkMaterial, barkColor).harvestTool(ToolType.AXE).hardnessAndResistance(2.0f).sound(barkSound)), name + "_wood");

// doors
DoorBlock door = register(new WoodenDoorBlock(AbstractBlock.Properties.create(planksMaterial, planksColor).harvestTool(planksTool).hardnessAndResistance(3.0F).sound(plankSound).notSolid()), name + "_door");
TrapDoorBlock trapdoor = register(new TrapDoorBlock(AbstractBlock.Properties.create(planksMaterial, planksColor).harvestTool(planksTool).hardnessAndResistance(3.0F).sound(SoundType.WOOD).notSolid().setAllowsSpawn(Blocks::neverAllowSpawn)), name + "_trapdoor");
FenceGateBlock fenceGate = register(new FenceGateBlock(planksProps), name + "_fence_gate");
// redstone
AbstractBlock.Properties redstoneProps = AbstractBlock.Properties.create(planksMaterial, planksColor).harvestTool(planksTool).doesNotBlockMovement().hardnessAndResistance(0.5F).sound(plankSound);
PressurePlateBlock pressurePlate = register(new PressurePlateBlock(Sensitivity.EVERYTHING, redstoneProps), name + "_pressure_plate");
WoodButtonBlock button = register(new WoodButtonBlock(redstoneProps), name + "_button");
// signs
StandingSignBlock standingSign = register(new StandingSignBlock(AbstractBlock.Properties.create(planksMaterial, planksColor).doesNotBlockMovement().hardnessAndResistance(1.0F).sound(plankSound), woodType), name + "_sign");
WallSignBlock wallSign = register(new WallSignBlock(AbstractBlock.Properties.create(planksMaterial, planksColor).doesNotBlockMovement().hardnessAndResistance(1.0F).sound(plankSound).lootFrom(standingSign.delegate), woodType), name + "_wall_sign");
// tell mantle to inject these into the TE
RegistrationHelper.registerSignBlock(standingSign.delegate);
RegistrationHelper.registerSignBlock(wallSign.delegate);
// finally, return
return new WoodBlockObject(getResource(name), woodType, planks, log, strippedLog, wood, strippedWood, fence, fenceGate, door, trapdoor, pressurePlate, button, standingSign, wallSign);
}

/* Fluid */

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package slimeknights.mantle.registration.adapter;

import net.minecraft.block.Block;
import net.minecraft.data.BlockStateVariantBuilder.ITriFunction;
import net.minecraft.entity.EntityType;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BucketItem;
import net.minecraft.item.Item;
import net.minecraft.item.Item.Properties;
import net.minecraft.item.SignItem;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.item.TallBlockItem;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.item.BlockTooltipItem;
import slimeknights.mantle.item.BurnableBlockItem;
import slimeknights.mantle.item.BurnableSignItem;
import slimeknights.mantle.item.BurnableTallBlockItem;
import slimeknights.mantle.item.TooltipItem;
import slimeknights.mantle.registration.ItemProperties;
import slimeknights.mantle.registration.object.BuildingBlockObject;
import slimeknights.mantle.registration.object.EnumObject;
import slimeknights.mantle.registration.object.FenceBuildingBlockObject;
import slimeknights.mantle.registration.object.WallBuildingBlockObject;
import slimeknights.mantle.registration.object.WoodBlockObject;

import javax.annotation.Nullable;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -174,6 +181,47 @@ public void registerDefaultBlockItem(FenceBuildingBlockObject object) {
registerDefaultBlockItem(object.getFence());
}

/**
* Registers block items for all entries in a fence building block object
* @param object Building block object instance
*/
@SuppressWarnings("ConstantConditions")
public void registerDefaultBlockItem(WoodBlockObject object, boolean isBurnable) {
// many of these are already burnable via tags, but simplier to set them all here
BiFunction<? super Block, Integer, ? extends BlockItem> burnableItem;
Function<? super Block, ? extends BlockItem> burnableTallItem;
ITriFunction<Item.Properties, ? super Block, ? super Block, ? extends BlockItem> burnableSignItem;
if (isBurnable) {
burnableItem = (block, burnTime) -> new BurnableBlockItem(block, defaultProps, burnTime);
burnableTallItem = (block) -> new BurnableTallBlockItem(block, defaultProps, 200);
burnableSignItem = (props, standing, wall) -> new BurnableSignItem(props, standing, wall, 200);
} else {
burnableItem = (block, burnTime) -> new BlockItem(block, defaultProps);
burnableTallItem = (block) -> new TallBlockItem(block, defaultProps);
burnableSignItem = SignItem::new;
}

// planks
BlockItem planks = registerBlockItem(burnableItem.apply(object.get(), 300));
registerBlockItem(burnableItem.apply(object.getSlab(), 150));
registerBlockItem(burnableItem.apply(object.getStairs(), 300));
registerBlockItem(burnableItem.apply(object.getFence(), 300));
// logs and wood
registerBlockItem(burnableItem.apply(object.getLog(), 300));
registerBlockItem(burnableItem.apply(object.getWood(), 300));
registerBlockItem(burnableItem.apply(object.getStrippedLog(), 300));
registerBlockItem(burnableItem.apply(object.getStrippedWood(), 300));
// doors
registerBlockItem(burnableTallItem.apply(object.getDoor()));
registerBlockItem(burnableItem.apply(object.getTrapdoor(), 300));
registerBlockItem(burnableItem.apply(object.getFenceGate(), 300));
// redstone
registerBlockItem(burnableItem.apply(object.getPressurePlate(), 300));
registerBlockItem(burnableItem.apply(object.getButton(), 100));
// sign
registerBlockItem(burnableSignItem.apply(new Item.Properties().maxStackSize(16).group(planks.getGroup()), object.getSign(), object.getWallSign()));
}

/**
* Registers block items for an enum object
* @param enumObject Enum object instance
Expand Down
Loading

0 comments on commit 542851f

Please sign in to comment.