Skip to content

Commit

Permalink
Placeholder previews & start of placement handlers
Browse files Browse the repository at this point in the history
Part of #38.

 - Wood placeholder previews
 - Start of wood placement handlers

Todo: door bug, testing inventories, fix lectern, stone/soil support, handle dynamic wood variants in placement handler (further testing needed)
  • Loading branch information
natrow committed Jun 26, 2023
1 parent c888d0c commit 531c64d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/natrow/tfc_minecolonies/TFCMConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.stream.Collectors;
import com.natrow.tfc_minecolonies.block.TFCMBlocks;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
Expand All @@ -21,6 +22,7 @@ public class TFCMConstants
public static final Lazy<Map<Block, ItemStack>> LOG_TO_SAPLINGS;
public static final Lazy<Map<Block, Block>> ANVIL_TO_ROCK;
public static final Lazy<Map<Block, Block>> SOIL_TO_FARMLAND;
public static final Lazy<Map<Block, Map<String, Block>>> PLACEHOLDER_TO_WOOD;
public static final String DROPDOWN_WOOD_ID = "woodType";
public static final String BUTTON_NEXT_WOOD_ID = "nextWoodType";
public static final String BUTTON_PREVIOUS_WOOD_ID = "previousWoodType";
Expand All @@ -46,6 +48,7 @@ public class TFCMConstants
.stream()
.flatMap(e -> e.entrySet().stream())
.collect(Collectors.toMap(e -> e.getValue().get(), e -> TFCBlocks.SOIL.get(SoilBlockType.FARMLAND).get(e.getKey()).get())));
PLACEHOLDER_TO_WOOD = Lazy.of(() -> TFCMBlocks.PLACEHOLDER_WOODS.entrySet().stream().collect(Collectors.toMap(e -> e.getValue().get(), e -> TFCBlocks.WOODS.entrySet().stream().collect(Collectors.toMap(e2 -> e2.getKey().getSerializedName(), e2 -> e2.getValue().get(e.getKey()).get())))));
}

public static ResourceLocation getResourceLocation(String resource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Locale;
import java.util.function.Supplier;
import com.natrow.tfc_minecolonies.TFCMConstants;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.material.Material;
Expand Down Expand Up @@ -31,11 +32,12 @@ public static Supplier<Block> create(Wood.BlockType type, RegistryWood wood)
}
else if (type == Wood.BlockType.LOOM)
{
return () -> new TFCLoomBlock(ExtendedProperties.of(Material.WOOD, wood.woodColor()).sound(SoundType.WOOD).strength(2.5F).noOcclusion().flammableLikePlanks().blockEntity(TFCBlockEntities.LOOM).ticks(LoomBlockEntity::tick), TFCMConstants.getResourceLocation("block/wood/planks/" + wood.getSerializedName()));
return () -> new TFCLoomBlock(ExtendedProperties.of(Material.WOOD, wood.woodColor()).sound(SoundType.WOOD).strength(2.5F).noOcclusion().flammableLikePlanks().blockEntity(TFCBlockEntities.LOOM).ticks(LoomBlockEntity::tick), new ResourceLocation("tfc:block/wood/planks/oak"));
}

return type.create(wood);
}

private final String serializedName;
private final MaterialColor woodColor;
private final MaterialColor barkColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.natrow.tfc_minecolonies.mixin;

import java.util.Locale;
import com.ldtteam.structurize.client.BlueprintRenderer;
import com.ldtteam.structurize.helpers.Settings;
import com.natrow.tfc_minecolonies.TFCMConstants;
import com.natrow.tfc_minecolonies.structurize.ISettingsExtension;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(value = BlueprintRenderer.class, remap = false)
public abstract class BlueprintRendererMixin implements AutoCloseable
{
@ModifyVariable(method = "init", name = "state", at = @At("STORE"))
BlockState initInjector(BlockState value)
{
if (Settings.instance.renderLightPlaceholders() && TFCMConstants.PLACEHOLDER_TO_WOOD.get().containsKey(value.getBlock()))
{
final String woodType = ((ISettingsExtension) (Object) Settings.instance).getWoodType().toLowerCase(Locale.ROOT);
final Block targetBlock = TFCMConstants.PLACEHOLDER_TO_WOOD.get().get(value.getBlock()).get(woodType);
return targetBlock.withPropertiesOf(value);
}
else
{
return value;
}
}
}
27 changes: 17 additions & 10 deletions src/main/java/com/natrow/tfc_minecolonies/mixin/SettingsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.natrow.tfc_minecolonies.structurize.ISettingsExtension;
import net.minecraft.nbt.CompoundTag;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -17,40 +18,46 @@ public abstract class SettingsMixin implements ISettingsExtension
private String rockType = "";
private String soilType = "";

@Shadow
public abstract void scheduleRefresh();

@Override
public String getWoodType()
{
return woodType;
}

@Override
public String getSoilType()
public void setWoodType(String woodType)
{
return soilType;
this.woodType = woodType;
scheduleRefresh();
}

@Override
public void setSoilType(String soilType)
public String getRockType()
{
this.soilType = soilType;
return rockType;
}

@Override
public void setWoodType(String woodType)
public void setRockType(String rockType)
{
this.woodType = woodType;
this.rockType = rockType;
scheduleRefresh();
}

@Override
public String getRockType()
public String getSoilType()
{
return rockType;
return soilType;
}

@Override
public void setRockType(String rockType)
public void setSoilType(String soilType)
{
this.rockType = rockType;
this.soilType = soilType;
scheduleRefresh();
}

@Inject(method = "deserializeNBT(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler;
import com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers;
import com.ldtteam.structurize.util.PlacementSettings;
import com.natrow.tfc_minecolonies.TFCMConstants;
import com.natrow.tfc_minecolonies.item.TFCMItems;
import net.minecraft.core.BlockPos;
Expand All @@ -15,6 +16,7 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BedPart;
import org.jetbrains.annotations.Nullable;
Expand All @@ -32,6 +34,7 @@
import net.dries007.tfc.common.blocks.soil.FarmlandBlock;
import net.dries007.tfc.common.blocks.soil.ISoilBlock;
import net.dries007.tfc.common.blocks.soil.PathBlock;
import net.dries007.tfc.common.blocks.wood.Wood;
import net.dries007.tfc.common.items.HideItemType;
import net.dries007.tfc.common.items.TFCItems;
import net.dries007.tfc.util.Helpers;
Expand All @@ -54,6 +57,7 @@ public static void registerHandlers()
PlacementHandlers.add(new TFCFirepitPlacementHandler());
PlacementHandlers.add(new TFCForgeHandler());
PlacementHandlers.add(new TFCTileEntityPlacementHandler());
PlacementHandlers.add(new TFCPlaceholderWoodHandler());
}

/**
Expand Down Expand Up @@ -341,4 +345,67 @@ public List<ItemStack> getRequiredItems(Level level, BlockPos blockPos, BlockSta
return itemList;
}
}

public static class TFCPlaceholderWoodHandler implements IPlacementHandler
{
@Override
public boolean canHandle(Level level, BlockPos blockPos, BlockState blockState)
{
return TFCMConstants.PLACEHOLDER_TO_WOOD.get().containsKey(blockState.getBlock());
}

@Override
public ActionProcessingResult handle(Level world, BlockPos pos, BlockState blockState, @Nullable CompoundTag tileEntityData, boolean complete, BlockPos centerPos, PlacementSettings settings)
{
if (complete)
{
world.setBlock(pos, blockState, UPDATE_FLAG);
return ActionProcessingResult.PASS;
}

final String woodType = Wood.ASH.getSerializedName(); // todo
final Block targetBlock = TFCMConstants.PLACEHOLDER_TO_WOOD.get().get(blockState.getBlock()).get(woodType);

final BlockState targetState = targetBlock.withPropertiesOf(blockState);

for (final IPlacementHandler placementHandler : PlacementHandlers.handlers)
{
if (placementHandler.canHandle(world, pos, targetState))
{
return placementHandler.handle(world, pos, targetState, tileEntityData, false, centerPos, settings);
}
}

throw new RuntimeException("Couldn't find a valid placement handler for placeholder block");
}

@Override
public List<ItemStack> getRequiredItems(Level world, BlockPos pos, BlockState blockState, @Nullable CompoundTag tileEntityData, boolean complete)
{

if (complete)
{
List<ItemStack> itemList = new ArrayList<>();
itemList.add(new ItemStack(blockState.getBlock()));
return itemList;
}
else
{
final String woodType = Wood.ASH.getSerializedName(); // todo
final Block targetBlock = TFCMConstants.PLACEHOLDER_TO_WOOD.get().get(blockState.getBlock()).get(woodType);

final BlockState targetState = targetBlock.withPropertiesOf(blockState);

for (final IPlacementHandler placementHandler : PlacementHandlers.handlers)
{
if (placementHandler.canHandle(world, pos, targetState))
{
return placementHandler.getRequiredItems(world, pos, targetBlock.withPropertiesOf(blockState), tileEntityData, false);
}
}
}

throw new RuntimeException("Couldn't find a valid placement handler for placeholder block");
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mixins.tfc_minecolonies.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"mixins": ["AbstractBlockHutMixin", "BlockEntityTypeAccessor", "ColonyPermissionEventHandlerMixin", "ItemStackUtilsMixin", "NetworkChannelMixin", "SettingsMixin", "TFCChestBlockEntityRendererMixin", "TileEntityColonyBuildingMixin", "ToolMixin", "ToolTypeMixin", "TreeMixin", "WorkerUtilMixin", "AI.AbstractEntityAIInteractMixin", "AI.EntityAIWorkFarmerMixin", "AI.EntityAIWorkLumberjackMixin"],
"refmap": "mixins.tfc_minecolonies.refmap.json",
"compatibilityLevel": "JAVA_17",
"client": ["WindowBuildToolMixin", "WindowMinecoloniesBuildToolMixin"]
"client": ["BlueprintRendererMixin", "WindowBuildToolMixin", "WindowMinecoloniesBuildToolMixin"]
}

0 comments on commit 531c64d

Please sign in to comment.