Skip to content

Commit 997da93

Browse files
authored
Merge branch 'master' into master
2 parents bbcf3a8 + 7cda901 commit 997da93

16 files changed

Lines changed: 706 additions & 129 deletions

dependencies.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Add your dependencies here
22

33
dependencies {
4-
api("com.github.GTNewHorizons:GTNHLib:0.6.13:dev")
5-
runtimeOnly("com.github.GTNewHorizons:NotEnoughItems:2.7.29-GTNH:dev")
4+
api("com.github.GTNewHorizons:GTNHLib:0.6.17:dev")
5+
implementation("com.github.GTNewHorizons:NotEnoughItems:2.7.29-GTNH:dev")
66

77
compileOnly("com.github.GTNewHorizons:ServerUtilities:2.1.41-pre")
88

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.gtnewhorizon.structurelib;
2+
3+
import java.util.AbstractMap;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.Locale;
9+
import java.util.Map;
10+
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
13+
import net.minecraft.item.ItemStack;
14+
15+
import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
16+
17+
import cpw.mods.fml.common.Loader;
18+
19+
// this is not an API! go check StructureLibAPI instead!
20+
public class ChannelDescription {
21+
22+
private static final ItemStackMap<Set<ChannelDescription>> itemToChannels = new ItemStackMap<>(true);
23+
private static final Map<String, ChannelDescription> registry = new HashMap<>();
24+
private final String channel;
25+
private final Map<String, String> descriptions = new HashMap<>();
26+
private final ItemStackMap<Integer> items = new ItemStackMap<>(true);
27+
28+
public static ChannelDescription get(final String channel) {
29+
if (StructureLibAPI.isDebugEnabled() && !channel.toLowerCase(Locale.ROOT).equals(channel))
30+
throw new IllegalArgumentException("Channel name can be lower case ONLY");
31+
return registry.computeIfAbsent(channel, ChannelDescription::new);
32+
}
33+
34+
public static boolean has(final String channel) {
35+
if (StructureLibAPI.isDebugEnabled() && !channel.toLowerCase(Locale.ROOT).equals(channel))
36+
throw new IllegalArgumentException("Channel name can be lower case ONLY");
37+
return registry.containsKey(channel);
38+
}
39+
40+
public static Collection<Map.Entry<String, Integer>> iterate(final ItemStack block) {
41+
Set<ChannelDescription> channels = itemToChannels.get(block);
42+
if (channels == null) return Collections.emptySet();
43+
return channels.stream().map(d -> new AbstractMap.SimpleImmutableEntry<>(d.channel, d.items.get(block)))
44+
.collect(Collectors.toList());
45+
}
46+
47+
public static void set(final String channel, final String modid, final String description) {
48+
if (StructureLibAPI.isDebugEnabled() && !channel.toLowerCase(Locale.ROOT).equals(channel))
49+
throw new IllegalArgumentException("Channel name can be lower case ONLY");
50+
registry.computeIfAbsent(channel, ChannelDescription::new).add(modid, description);
51+
}
52+
53+
public static void item(final String channel, int channelValue, ItemStack stack) {
54+
if (StructureLibAPI.isDebugEnabled()) {
55+
if (!channel.toLowerCase(Locale.ROOT).equals(channel)) {
56+
throw new IllegalArgumentException("Channel name can be lower case ONLY");
57+
}
58+
if (channelValue <= 0) {
59+
throw new IllegalArgumentException("Channel value must be greater than 0");
60+
}
61+
}
62+
ChannelDescription inst = registry.computeIfAbsent(channel, ChannelDescription::new);
63+
inst.items.put(stack, channelValue);
64+
itemToChannels.computeIfAbsent(stack, s -> new HashSet<>()).add(inst);
65+
}
66+
67+
public static Map<String, ChannelDescription> getAll() {
68+
return Collections.unmodifiableMap(registry);
69+
}
70+
71+
private ChannelDescription(String channel) {
72+
this.channel = channel;
73+
}
74+
75+
public String getChannel() {
76+
return channel;
77+
}
78+
79+
public ChannelDescription add(String modid, String description) {
80+
if (Loader.instance().getIndexedModList().get(modid) == null) {
81+
throw new IllegalArgumentException("Mod not found: " + modid);
82+
}
83+
descriptions.put(modid, description);
84+
return this;
85+
}
86+
87+
public Map<String, String> getDescriptions() {
88+
return Collections.unmodifiableMap(descriptions);
89+
}
90+
91+
public Map<ItemStack, Integer> getItems() {
92+
return Collections.unmodifiableMap(items);
93+
}
94+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.gtnewhorizon.structurelib;
2+
3+
import net.minecraft.init.Blocks;
4+
import net.minecraft.item.ItemStack;
5+
import net.minecraft.nbt.NBTTagCompound;
6+
import net.minecraft.nbt.NBTTagList;
7+
8+
import cpw.mods.fml.common.event.FMLInterModComms;
9+
10+
public class DevelopHelper {
11+
12+
private static final boolean DEBUG = Boolean.getBoolean("structurelib.develop_helper");
13+
14+
public static void onPreInit() {
15+
if (!DEBUG) return;
16+
StructureLib.LOGGER.info("Running develop helper");
17+
StructureLibAPI.registerChannelDescription("sandstone", StructureLibAPI.MOD_ID, "variants of Sandstone");
18+
StructureLibAPI.registerChannelItem("sandstone", StructureLibAPI.MOD_ID, 1, new ItemStack(Blocks.sandstone));
19+
StructureLibAPI
20+
.registerChannelItem("sandstone", StructureLibAPI.MOD_ID, 2, new ItemStack(Blocks.sandstone, 1, 1));
21+
StructureLibAPI
22+
.registerChannelItem("sandstone", StructureLibAPI.MOD_ID, 3, new ItemStack(Blocks.sandstone, 1, 2));
23+
StructureLibAPI.registerChannelDescription("sandbricks", StructureLibAPI.MOD_ID, "variants of Sandbricks");
24+
StructureLibAPI.registerChannelDescription("apple", StructureLibAPI.MOD_ID, "apple");
25+
StructureLibAPI.registerChannelDescription("bright", StructureLibAPI.MOD_ID, "bright");
26+
StructureLibAPI.registerChannelDescription("apple_blight", StructureLibAPI.MOD_ID, "apple_blight");
27+
StructureLibAPI.registerChannelDescription("app_eagle", StructureLibAPI.MOD_ID, "app_eagle");
28+
}
29+
30+
public void x() {
31+
NBTTagCompound command = new NBTTagCompound();
32+
NBTTagList items = new NBTTagList();
33+
int[] metas = { 0, 1, 2 };
34+
for (int meta : metas) {
35+
NBTTagCompound tag = new NBTTagCompound();
36+
tag.setTag("Item", new ItemStack(Blocks.sandstone, 1, meta).writeToNBT(new NBTTagCompound()));
37+
NBTTagList channels = new NBTTagList();
38+
NBTTagCompound channel = new NBTTagCompound();
39+
channel.setString("Channel", "sandstone");
40+
channel.setInteger("Value", meta + 1);
41+
channels.appendTag(channel);
42+
tag.setTag("Channels", channels);
43+
items.appendTag(tag);
44+
}
45+
command.setTag("Items", items);
46+
FMLInterModComms.sendMessage("structurelib", "register_channel_item", command);
47+
}
48+
}

src/main/java/com/gtnewhorizon/structurelib/GuiHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.minecraft.entity.player.EntityPlayer;
44
import net.minecraft.world.World;
55

6+
import com.gtnewhorizon.structurelib.gui.EmptyContainer;
67
import com.gtnewhorizon.structurelib.gui.GuiScreenConfigureChannels;
78

89
import cpw.mods.fml.common.network.IGuiHandler;
@@ -11,14 +12,19 @@ public class GuiHandler implements IGuiHandler {
1112

1213
@Override
1314
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
14-
return null;
15+
switch (ID) {
16+
case 0:
17+
return new EmptyContainer(player);
18+
default:
19+
return null;
20+
}
1521
}
1622

1723
@Override
1824
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
1925
switch (ID) {
2026
case 0:
21-
return new GuiScreenConfigureChannels(player.inventory.getStackInSlot(x));
27+
return new GuiScreenConfigureChannels(new EmptyContainer(player), player.inventory.getStackInSlot(x));
2228
default:
2329
return null;
2430
}

src/main/java/com/gtnewhorizon/structurelib/StructureLib.java

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.gtnewhorizon.structurelib;
22

3+
import static com.gtnewhorizon.structurelib.StructureLibAPI.CHANNEL_SHOW_ERROR;
4+
import static com.gtnewhorizon.structurelib.StructureLibAPI.MOD_ID;
5+
36
import net.minecraft.block.Block;
47
import net.minecraft.creativetab.CreativeTabs;
58
import net.minecraft.entity.player.EntityPlayer;
69
import net.minecraft.item.Item;
710
import net.minecraft.item.ItemBlock;
11+
import net.minecraft.item.ItemStack;
812
import net.minecraft.launchwrapper.Launch;
13+
import net.minecraft.nbt.NBTTagCompound;
14+
import net.minecraft.nbt.NBTTagList;
15+
import net.minecraftforge.common.util.Constants;
916

1017
import org.apache.logging.log4j.LogManager;
1118
import org.apache.logging.log4j.Logger;
@@ -24,10 +31,10 @@
2431
import com.gtnewhorizon.structurelib.util.InventoryUtility;
2532
import com.gtnewhorizon.structurelib.util.XSTR;
2633

27-
import cpw.mods.fml.common.Loader;
2834
import cpw.mods.fml.common.Mod;
2935
import cpw.mods.fml.common.SidedProxy;
3036
import cpw.mods.fml.common.event.FMLInitializationEvent;
37+
import cpw.mods.fml.common.event.FMLInterModComms;
3138
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
3239
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
3340
import cpw.mods.fml.common.event.FMLServerStartingEvent;
@@ -41,7 +48,7 @@
4148
* This class does not contain a stable API. Refrain from using this class.
4249
*/
4350
@Mod(
44-
modid = StructureLibAPI.MOD_ID,
51+
modid = MOD_ID,
4552
name = "StructureLib",
4653
version = Tags.VERSION,
4754
acceptableRemoteVersions = "*",
@@ -58,7 +65,7 @@ public class StructureLib {
5865
clientSide = "com.gtnewhorizon.structurelib.ClientProxy")
5966
static CommonProxy proxy;
6067

61-
static final SimpleNetworkWrapper net = NetworkRegistry.INSTANCE.newSimpleChannel(StructureLibAPI.MOD_ID);
68+
static final SimpleNetworkWrapper net = NetworkRegistry.INSTANCE.newSimpleChannel(MOD_ID);
6269

6370
static {
6471
net.registerMessage(
@@ -85,7 +92,7 @@ public class StructureLib {
8592

8693
@Mod.Instance
8794
static StructureLib INSTANCE;
88-
95+
@Mod.Instance(STRUCTURECOMPAT_MODID)
8996
static Object COMPAT;
9097

9198
static Block blockHint;
@@ -101,8 +108,6 @@ public Item getTabIconItem() {
101108
}
102109
};
103110

104-
public static boolean isGTLoaded;
105-
106111
@Mod.EventHandler
107112
public void preInit(FMLPreInitializationEvent e) {
108113
ConfigurationHandler.INSTANCE.init(e.getSuggestedConfigurationFile());
@@ -118,11 +123,10 @@ public void preInit(FMLPreInitializationEvent e) {
118123
NetworkRegistry.INSTANCE.registerGuiHandler(instance(), new GuiHandler());
119124

120125
InventoryUtility.init();
121-
if (Loader.isModLoaded(STRUCTURECOMPAT_MODID)) {
122-
COMPAT = Loader.instance().getIndexedModList().get(STRUCTURECOMPAT_MODID).getMod();
123-
}
124126

125-
isGTLoaded = Loader.isModLoaded("gregtech");
127+
ChannelDescription.set(CHANNEL_SHOW_ERROR, MOD_ID, "channels.structurelib.show_errors");
128+
129+
DevelopHelper.onPreInit();
126130
}
127131

128132
@Mod.EventHandler
@@ -141,6 +145,48 @@ public void serverStarting(FMLServerStartingEvent e) {
141145
e.registerServerCommand(new CommandRegistryDebug());
142146
}
143147

148+
@Mod.EventHandler
149+
public void handleIMC(FMLInterModComms.IMCEvent event) {
150+
for (FMLInterModComms.IMCMessage message : event.getMessages()) {
151+
switch (message.key) {
152+
case "register_channel":
153+
processRegisterChannel(message);
154+
break;
155+
case "register_channel_item":
156+
processRegisterChannelItem(message);
157+
break;
158+
}
159+
}
160+
}
161+
162+
private void processRegisterChannel(FMLInterModComms.IMCMessage message) {
163+
NBTTagList tags = message.getNBTValue().getTagList("Channels", Constants.NBT.TAG_COMPOUND);
164+
for (int i = 0; i < tags.tagCount(); i++) {
165+
NBTTagCompound tag = tags.getCompoundTagAt(i);
166+
StructureLibAPI.registerChannelDescription(
167+
tag.getString("Channel"),
168+
message.getSender(),
169+
tag.getString("Description"));
170+
}
171+
}
172+
173+
private void processRegisterChannelItem(FMLInterModComms.IMCMessage message) {
174+
NBTTagList items = message.getNBTValue().getTagList("Items", Constants.NBT.TAG_COMPOUND);
175+
for (int i = 0; i < items.tagCount(); i++) {
176+
NBTTagCompound itemTag = items.getCompoundTagAt(i);
177+
ItemStack item = ItemStack.loadItemStackFromNBT(itemTag.getCompoundTag("Item"));
178+
NBTTagList tags = itemTag.getTagList("Channels", Constants.NBT.TAG_COMPOUND);
179+
for (int j = 0; j < tags.tagCount(); j++) {
180+
NBTTagCompound tag = tags.getCompoundTagAt(j);
181+
StructureLibAPI.registerChannelItem(
182+
tag.getString("Channel"),
183+
message.getSender(),
184+
tag.getInteger("Value"),
185+
item);
186+
}
187+
}
188+
}
189+
144190
public static void addClientSideChatMessages(String... messages) {
145191
proxy.addClientSideChatMessages(messages);
146192
}

src/main/java/com/gtnewhorizon/structurelib/StructureLibAPI.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.minecraft.util.IIcon;
1212
import net.minecraft.world.World;
1313

14+
import com.gtnewhorizon.gtnhlib.util.AnimatedTooltipHandler;
1415
import com.gtnewhorizon.structurelib.alignment.IAlignment;
1516
import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider;
1617
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
@@ -43,6 +44,7 @@ public class StructureLibAPI {
4344
public static final int HINT_BLOCK_META_AIR = 13;
4445
public static final int HINT_BLOCK_META_NOT_AIR = 14;
4546
public static final int HINT_BLOCK_META_ERROR = 15;
47+
public static final String CHANNEL_SHOW_ERROR = "show_error";
4648
static final ThreadLocal<Object> instrument = new ThreadLocal<>();
4749

4850
/**
@@ -350,4 +352,37 @@ public static void addThrottledChat(Object throttleKey, EntityPlayer player, ICh
350352
short intervalRequired, boolean forceUpdateLastSend) {
351353
proxy.addThrottledChat(throttleKey, player, text, intervalRequired, forceUpdateLastSend);
352354
}
355+
356+
/**
357+
* Register a channel description here. The description should describe how a channel will be used inside your mod.
358+
*
359+
* @param channel channel key
360+
* @param modid your modid
361+
* @param description localization key for your description
362+
*/
363+
public static void registerChannelDescription(final String channel, final String modid, final String description) {
364+
ChannelDescription.set(channel, modid, description);
365+
}
366+
367+
/**
368+
* Register an item stack as an indicator for a (channel, value) pair.
369+
*
370+
* @param channel channel key
371+
* @param modid your modid
372+
* @param channelValue value
373+
* @param stack item stack. having a metadata of
374+
* {@link net.minecraftforge.oredict.OreDictionary#WILDCARD_VALUE} will match without comparing
375+
* NBT or metadata value. having a tag with {@code tag.getBoolean("*") == true} will match
376+
* without comparing NBT.
377+
*/
378+
public static void registerChannelItem(final String channel, final String modid, final int channelValue,
379+
final ItemStack stack) {
380+
ChannelDescription.item(channel, channelValue, stack);
381+
AnimatedTooltipHandler.addItemTooltip(
382+
stack,
383+
AnimatedTooltipHandler.translatedText("structurelib.tooltip.channelvalue", channelValue, channel));
384+
AnimatedTooltipHandler.addItemTooltip(
385+
stack,
386+
AnimatedTooltipHandler.translatedText("structurelib.tooltip.indicator_dnd", channelValue, channel));
387+
}
353388
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gtnewhorizon.structurelib.gui;
2+
3+
import net.minecraft.entity.player.EntityPlayer;
4+
import net.minecraft.inventory.Container;
5+
6+
public class EmptyContainer extends Container {
7+
8+
private final EntityPlayer player;
9+
10+
public EmptyContainer(EntityPlayer player) {
11+
this.player = player;
12+
}
13+
14+
@Override
15+
public boolean canInteractWith(EntityPlayer player) {
16+
return player == this.player;
17+
}
18+
}

0 commit comments

Comments
 (0)