Skip to content

Commit

Permalink
Waila Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Sep 24, 2019
1 parent fd86782 commit 23f3c95
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/slimeknights/tmechworks/TMechworks.java
Expand Up @@ -11,7 +11,6 @@
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.network.NetworkRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import slimeknights.mantle.util.ModelJsonGenerator;
Expand All @@ -23,6 +22,7 @@
@Mod(TMechworks.modId)
public class TMechworks {
public static final String modId = "tmechworks";
public static final String modName = "Tinkers' Mechworks";

public static final Logger log = LogManager.getLogger(modId);

Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.container.Container;
Expand All @@ -15,7 +16,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.util.text.*;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext;
Expand Down Expand Up @@ -535,6 +536,35 @@ public FakePlayer getFakePlayer(BlockPos pos) {
return fakePlayer.get();
}

@Override
public void getInformation(@Nonnull List<ITextComponent> info, InformationType type, CompoundNBT serverData, PlayerEntity player) {
super.getInformation(info, type, serverData, player);

if (type != InformationType.BODY) {
return;
}

info.add(new TranslationTextComponent(Util.prefix("machine.stats")));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.stats.advanced"), stats.isAdvanced));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.stats.length"), stats.extendLength));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.stats.delay"), stats.extendDelay));
info.add(new StringTextComponent(""));

requireSneak(info, player, () -> {
info.add(new TranslationTextComponent(Util.prefix("machine.state")));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.state.moving"), serverData.getBoolean("moving")));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.state.extended"), serverData.getBoolean("extended")));
info.add(new TranslationTextComponent(Util.prefix("drawbridge.state.extendedcount"), serverData.getInt("extendedCount")));
});
}

@Override
public void syncInformation(CompoundNBT nbt, ServerPlayerEntity player) {
nbt.putBoolean("extended", isExtended);
nbt.putBoolean("moving", isMoving);
nbt.putInt("extendedCount", extendedLength);
}

/**
* Adaptation of ForgeHooks.onPlaceItemIntoWorld that passes the context straight into tryPlace
*/
Expand Down
Expand Up @@ -3,11 +3,13 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.blocks.FirestarterBlock;
Expand Down Expand Up @@ -63,15 +65,15 @@ public ItemStack storeTileData(ItemStack stack) {
}

@Override
public void getInformation(@Nonnull List<String> info, InformationType type) {
super.getInformation(info, type);
public void getInformation(@Nonnull List<ITextComponent> info, InformationType type, PlayerEntity player) {
super.getInformation(info, type, player);
if(type != InformationType.BODY) {
return;
}

BlockState state = getWorld().getBlockState(getPos());
boolean shouldExtinguish = state.get(FirestarterBlock.EXTINGUISH);

info.add(I18n.format(Util.prefix("hud.behaviour")) + ": " + I18n.format(Util.prefix("hud.behaviour.firestarter." + (shouldExtinguish ? "extinguish" : "keep"))));
info.add(new TranslationTextComponent(Util.prefix("hud.behaviour"), I18n.format(Util.prefix("hud.behaviour.firestarter." + (shouldExtinguish ? "extinguish" : "keep")))));
}
}
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.thread.EffectiveSide;
import slimeknights.mantle.tileentity.InventoryTileEntity;
Expand Down Expand Up @@ -255,11 +256,16 @@ public ItemStack storeTileData(ItemStack stack) {
}

@Override
public void getInformation(@Nonnull List<String> info, InformationType type) {
public void getInformation(@Nonnull List<ITextComponent> info, InformationType type, PlayerEntity player) {
if(type != InformationType.BODY)
return;

info.add(I18n.format("hud.msg.power") + ": " + getRedstoneState());
info.add(new TranslationTextComponent("tooltip.waila.power", getRedstoneState()));
}

@Override
public void getInformation(@Nonnull List<ITextComponent> info, InformationType type, CompoundNBT serverData, PlayerEntity player) {
getInformation(info, type, player);
}

@Nullable
Expand Down
@@ -0,0 +1,45 @@
package slimeknights.tmechworks.integration.waila;

import mcp.mobius.waila.api.IComponentProvider;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.IServerDataProvider;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;

import java.util.List;

public class GenericTileDataProvider implements IServerDataProvider<TileEntity>, IComponentProvider {
public static final GenericTileDataProvider INSTANCE = new GenericTileDataProvider();

@Override
public void appendServerData(CompoundNBT nbt, ServerPlayerEntity player, World world, TileEntity tile) {
if(tile instanceof IInformationProvider)
((IInformationProvider)tile).syncInformation(nbt, player);
}

@Override
public void appendHead(List<ITextComponent> tooltip, IDataAccessor accessor, IPluginConfig config) {
call(tooltip, accessor, IInformationProvider.InformationType.HEAD);
}

@Override
public void appendBody(List<ITextComponent> tooltip, IDataAccessor accessor, IPluginConfig config) {
call(tooltip, accessor, IInformationProvider.InformationType.BODY);
}

@Override
public void appendTail(List<ITextComponent> tooltip, IDataAccessor accessor, IPluginConfig config) {
call(tooltip, accessor, IInformationProvider.InformationType.TAIL);
}

private void call(List<ITextComponent> tooltip, IDataAccessor accessor, IInformationProvider.InformationType type) {
TileEntity te = accessor.getTileEntity();

if(te instanceof IInformationProvider)
((IInformationProvider)te).getInformation(tooltip, type, accessor.getServerData(), accessor.getPlayer());
}
}
@@ -1,14 +1,42 @@
package slimeknights.tmechworks.integration.waila;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

public interface IInformationProvider {
@OnlyIn(Dist.CLIENT)
void getInformation(@Nonnull List<String> info, @Nonnull InformationType type);
default void getInformation(@Nonnull List<ITextComponent> info, @Nonnull InformationType type, PlayerEntity player) {

}

@OnlyIn(Dist.CLIENT)
default void getInformation(@Nonnull List<ITextComponent> info, @Nonnull InformationType type, CompoundNBT serverData, PlayerEntity player) {
getInformation(info, type, player);
}

default void syncInformation(CompoundNBT nbt, ServerPlayerEntity player) {

}

default void requireSneak(List<ITextComponent> tooltip, PlayerEntity player, Runnable action) {
if(!player.isSneaking()) {
tooltip.add(new TranslationTextComponent("tooltip.waila.sneak_for_details").applyTextStyle(TextFormatting.ITALIC));
} else {
action.run();
}
}

enum InformationType {
HEAD, BODY, TAIL
Expand Down
@@ -0,0 +1,23 @@
package slimeknights.tmechworks.integration.waila;

import mcp.mobius.waila.api.IRegistrar;
import mcp.mobius.waila.api.IWailaPlugin;
import mcp.mobius.waila.api.TooltipPosition;
import mcp.mobius.waila.api.WailaPlugin;
import net.minecraft.util.ResourceLocation;
import slimeknights.tmechworks.library.Util;

@WailaPlugin
public class WailaIntegration implements IWailaPlugin {
private static final ResourceLocation CONFIG_REDSTONE_MACHINE = Util.getResource("redstone_machine");

@Override
public void register(IRegistrar registrar) {
registrar.addConfig(CONFIG_REDSTONE_MACHINE, true);

registrar.registerComponentProvider(GenericTileDataProvider.INSTANCE, TooltipPosition.HEAD, IInformationProvider.class);
registrar.registerComponentProvider(GenericTileDataProvider.INSTANCE, TooltipPosition.BODY, IInformationProvider.class);
registrar.registerComponentProvider(GenericTileDataProvider.INSTANCE, TooltipPosition.TAIL, IInformationProvider.class);
registrar.registerBlockDataProvider(GenericTileDataProvider.INSTANCE, IInformationProvider.class);
}
}
18 changes: 15 additions & 3 deletions src/resources/assets/tmechworks/lang/en_us.json
Expand Up @@ -28,9 +28,21 @@
"itemGroup.TinkersMechworks": "Tinkers' Mechworks",

// HUD
"tmechworks:hud.behaviour": "Behaviour",
"tmechworks:hud.behaviour.firestarter.extinguish": "§9Extinguish",
"tmechworks:hud.behaviour.firestarter.keep": "§6Keep lit",
"tmechworks:tooltip.behaviour": "Behaviour: %s",
"tmechworks:tooltip.behaviour.firestarter.extinguish": "§9Extinguish",
"tmechworks:tooltip.behaviour.firestarter.keep": "§6Keep lit",

//// Stats
"tmechworks:machine.stats": "§lStats",
"tmechworks:drawbridge.stats.advanced": "Is Advanced: %s",
"tmechworks:drawbridge.stats.length": "Extend Distance: %d Blocks",
"tmechworks:drawbridge.stats.delay": "Extend Delay: %f",

//// State
"tmechworks:machine.state": "§lState",
"tmechworks:drawbridge.state.moving": "Is Moving: %s",
"tmechworks:drawbridge.state.extended": "Extended: %s",
"tmechworks:drawbridge.state.extendedcount": "Distance: %d Blocks",

// Inventory
"tmechworks:inventory.drawbridge": "Drawbridge",
Expand Down

0 comments on commit 23f3c95

Please sign in to comment.