Skip to content

Instantly share code, notes, and snippets.

@Joedobo27
Created December 18, 2016 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joedobo27/57c30ecaa66ac327dd4ca754c6a1011b to your computer and use it in GitHub Desktop.
Save Joedobo27/57c30ecaa66ac327dd4ca754c6a1011b to your computer and use it in GitHub Desktop.
package com.Joedobo27.disablefogspider;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.NotFoundException;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
import org.gotti.wurmunlimited.modloader.interfaces.Initable;
import org.gotti.wurmunlimited.modloader.interfaces.ServerStartedListener;
import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DisableFogSpider implements WurmServerMod, Initable, ServerStartedListener {
private static ClassPool classPool;
private static Logger logger;
@Override
public void init() {
try {
boolean fogSpiderDisabled = disableFogSpiderInPoll();
if (fogSpiderDisabled){
logger.log(Level.INFO, "Fog Spider creation disabled SUCCESSFUL.");
}
else {
logger.log(Level.INFO, "Fog Spider creation disabled FAILURE.");
}
}catch (NotFoundException | CannotCompileException e) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
@Override
public void onServerStarted() {
JAssistClassData.voidClazz();
}
/**
* Change Zone.poll() to disable the creation of Fog spiders.
* Use ExprEditor to change getFog() so it always returns Float.MIN_VALUE and thus disables creation.
* was--
* if (this.isOnSurface()) {
* if (Server.getWeather().getFog() > 0.5f && Zone.fogSpiders.size() < Zones.worldTileSizeX / 10) {
*
* Edit was on bytecode index 322 which goes with line number 534.
*
* @return boolean type. Was the change successful?
* @throws NotFoundException JA related, forwarded.
* @throws CannotCompileException JA related, forwarded.
*/
private boolean disableFogSpiderInPoll() throws NotFoundException, CannotCompileException {
final boolean[] toReturn = new boolean[]{false};
JAssistClassData forage = new JAssistClassData("com.wurmonline.server.zones.Zone", classPool);
JAssistMethodData poll = new JAssistMethodData(forage, "(I)V", "poll");
poll.getCtMethod().instrument(new ExprEditor() {
@Override
public void edit(MethodCall methodCall) throws CannotCompileException {
if (Objects.equals("getFog", methodCall.getMethodName()) && methodCall.getLineNumber() == 534){
logger.log(Level.FINE, "Zone.class, poll(), installed hook at line: " + methodCall.getLineNumber());
methodCall.replace("$_ = Float.MIN_VALUE;");
toReturn[0] = true;
}
}
});
return toReturn[0];
}
static {
classPool = HookManager.getInstance().getClassPool();
logger = Logger.getLogger(DisableFogSpider.class.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment