public Object[] setupIC(Block block, boolean create) {
// if we're not looking at a wall sign, it can't be an IC.
if (block.getType() != Material.WALL_SIGN) return null;
ChangedSign sign = BukkitUtil.toChangedSign(block);
// detect the text on the sign to see if it's any kind of IC at all.
Matcher matcher = RegexUtil.IC_PATTERN.matcher(sign.getLine(1));
if (!matcher.matches()) return null;
String prefix = matcher.group(2);
// TODO: remove after some time to stop converting existing MCA ICs
// convert existing MCA ICs to the new [MCXXXX]A syntax
if (prefix.equalsIgnoreCase("MCA")) {
sign.setLine(1, (StringUtils.replace(sign.getLine(1).toLowerCase(Locale.ENGLISH), "mca", "mc") + "a").toUpperCase(Locale.ENGLISH));
sign.update(false);
return setupIC(block, create);
}
if (sign.getLine(1).toLowerCase(Locale.ENGLISH).startsWith("[mc0")) {
if(sign.getLine(1).equalsIgnoreCase("[mc0420]"))
sign.setLine(1, "[MC1421]S");
else if(sign.getLine(1).equalsIgnoreCase("[mc0421]"))
sign.setLine(1, "[MC1422]S");
else
sign.setLine(1, (StringUtils.replace(sign.getLine(1).toLowerCase(Locale.ENGLISH), "mc0", "mc1") + "s").toUpperCase(Locale.ENGLISH));
sign.update(false);
return setupIC(block, create);
}
if (sign.getLine(1).toLowerCase(Locale.ENGLISH).startsWith("[mcz")) {
sign.setLine(1, (StringUtils.replace(sign.getLine(1).toLowerCase(Locale.ENGLISH), "mcz", "mcx") + "s").toUpperCase(Locale.ENGLISH));
sign.update(false);
return setupIC(block, create);
}
if (!manager.hasCustomPrefix(prefix)) return null;
String id = matcher.group(1);
if(disabledICs.contains(id.toLowerCase()) || disabledICs.contains(id)) return null; //This IC is disabled.
// after this point, we don't return null if we can't make an IC: we throw shit,
// because it SHOULD be an IC and can't possibly be any other kind of mechanic.
// now actually try to pull up an IC of that id number.
RegisteredICFactory registration = manager.get(id);
if (registration == null) {
CraftBookPlugin.logger().warning("\"" + sign.getLine(1) + "\" should be an IC ID, but no IC registered under that ID could be found.");
block.breakNaturally();
return null;
}
IC ic;
// check if the ic is cached and get that single instance instead of creating a new one
if (ICManager.isCachedIC(block.getLocation())) {
ic = ICManager.getCachedIC(block.getLocation());
if(ic.getSign().updateSign(sign)) {
ICManager.removeCachedIC(block.getLocation());
ic = registration.getFactory().create(sign);
if(!sign.getLine(0).equals(ic.getSignTitle()) && !sign.getLine(0).startsWith("=")) {
sign.setLine(0, ic.getSignTitle());
sign.update(false);
}
ic.load();
// add the created ic to the cache
ICManager.addCachedIC(block.getLocation(), ic);
}
} else if (create) {
ic = registration.getFactory().create(sign);
if(!sign.getLine(0).equals(ic.getSignTitle()) && !sign.getLine(0).startsWith("=")) {
sign.setLine(0, ic.getSignTitle());
sign.update(false);
}
ic.load();
// add the created ic to the cache
ICManager.addCachedIC(block.getLocation(), ic);
} else
return null;
// extract the suffix
String suffix = "";
String[] str = RegexUtil.RIGHT_BRACKET_PATTERN.split(sign.getLine(1));
if (str.length > 1) {
suffix = str[1];
}
ICFamily family = registration.getFamilies()[0];
if (suffix != null && !suffix.isEmpty()) {
for (ICFamily f : registration.getFamilies()) {
if (f.getSuffix().equalsIgnoreCase(suffix)) {
family = f;
break;
}
}
}
// okay, everything checked out. we can finally make it.
if (ic instanceof SelfTriggeredIC && (sign.getLine(1).trim().toUpperCase(Locale.ENGLISH).endsWith("S") || ((SelfTriggeredIC) ic).isAlwaysST()))
CraftBookPlugin.inst().getSelfTriggerManager().registerSelfTrigger(block.getLocation());
Object[] rets = new Object[3];
rets[0] = id;
rets[1] = family;