Skip to content

Commit

Permalink
Added a block breaker blacklist. Also tabs -> spaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe12o committed Apr 18, 2013
1 parent dc5857d commit 083453b
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 159 deletions.
15 changes: 15 additions & 0 deletions alicefixes/src/com/skcraft/alicefixes/AliceFixes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.skcraft.alicefixes;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = "aliceFixes", version = "0.1.0")
public class AliceFixes {

@PreInit
public void preInit(FMLPreInitializationEvent evt) {
BreakerBlacklist.load();
}

}
72 changes: 36 additions & 36 deletions alicefixes/src/com/skcraft/alicefixes/AliceTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@
import cpw.mods.fml.relauncher.IClassTransformer;

public class AliceTransformer implements IClassTransformer {
private final List<IClassTransformer> transformers;
public AliceTransformer() {
String[] names = LoadingPlugin.getTransformers();
transformers = new ArrayList(names.length);
for(String transformer : names) {
try {
transformers.add((IClassTransformer)Class.forName(transformer).newInstance());
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
@Override
public byte[] transform(String name, byte[] bytes) {
if(bytes == null) {
return bytes;
}
for(IClassTransformer transformer : transformers) {
try {
bytes = transformer.transform(name, bytes);
if(bytes == null)
FMLLog.log(Level.SEVERE, "Transformer " + transformer + " has corrupted class " + name);
}
catch(Throwable t) {
t.printStackTrace();
}
}
return bytes;
}
private final List<IClassTransformer> transformers;
public AliceTransformer() {
String[] names = LoadingPlugin.getTransformers();
transformers = new ArrayList(names.length);
for(String transformer : names) {
try {
transformers.add((IClassTransformer)Class.forName(transformer).newInstance());
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
@Override
public byte[] transform(String name, byte[] bytes) {
if(bytes == null) {
return bytes;
}
for(IClassTransformer transformer : transformers) {
try {
bytes = transformer.transform(name, bytes);
if(bytes == null)
FMLLog.log(Level.SEVERE, "Transformer " + transformer + " has corrupted class " + name);
}
catch(Throwable t) {
t.printStackTrace();
}
}
return bytes;
}

}
32 changes: 32 additions & 0 deletions alicefixes/src/com/skcraft/alicefixes/BreakerBlacklist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.skcraft.alicefixes;

import java.io.File;

import cpw.mods.fml.common.Loader;
import net.minecraftforge.common.Configuration;

public class BreakerBlacklist {

public static int[] forbiddenIds = new int[] {};

public static void load() {
File configDir = Loader.instance().getConfigDir();
configDir = new File(configDir, "/redpower/");
configDir.mkdir();
configDir = new File(configDir, "blacklist.cfg");
Configuration blacklist = new Configuration(configDir);

try {
blacklist.load();
forbiddenIds = blacklist.get("Blacklist", "forbiddenBlocks",
new int[] {1}, "List of block IDs that the block breaker cannot break. Add 1 ID per line.").getIntList();
}
catch(Throwable t) {
t.printStackTrace();
}
finally {
blacklist.save();
}
}

}
37 changes: 37 additions & 0 deletions alicefixes/src/com/skcraft/alicefixes/CoordHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.skcraft.alicefixes;

public class CoordHelper {

int x;
int y;
int z;

public CoordHelper(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public void addFacingAsOffset(int facing) {
switch (facing)
{
case 0:
this.y += 1;
break;
case 1:
this.y -= 1;
break;
case 2:
this.z += 1;
break;
case 3:
this.z -= 1;
break;
case 4:
this.x += 1;
break;
default:
this.x -= 1;
}
}
}
66 changes: 33 additions & 33 deletions alicefixes/src/com/skcraft/alicefixes/LoadingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@
@TransformerExclusions("com.skcraft.alicefixes")
public class LoadingPlugin implements IFMLLoadingPlugin {

@Override
public String[] getLibraryRequestClass() {
return null;
}

@Override
public String[] getASMTransformerClass() {
System.out.println("getASMTransformerClass()");
return new String[] { "com.skcraft.alicefixes.AliceTransformer" };
}

@Override
public String getModContainerClass() {
return null;
}

@Override
public String getSetupClass() {
return null;
}

@Override
public void injectData(Map<String, Object> data) {}

public static String[] getTransformers() {
return new String[] { "com.skcraft.alicefixes.TransformMiningLaser",
/*"com.skcraft.alicefixes.TransformIC2Explosions",
"com.skcraft.alicefixes.TransformTCExcWand",
"com.skcraft.alicefixes.TransformTCEquWand",
"com.skcraft.alicefixes.TransformTCFrostWand",
"com.skcraft.alicefixes.TransformTCAxe",
"com.skcraft.alicefixes.TransformTCShovel"*/};
}
@Override
public String[] getLibraryRequestClass() {
return null;
}

@Override
public String[] getASMTransformerClass() {
return new String[] { "com.skcraft.alicefixes.AliceTransformer" };
}

@Override
public String getModContainerClass() {
return null;
}

@Override
public String getSetupClass() {
return null;
}

@Override
public void injectData(Map<String, Object> data) {}

public static String[] getTransformers() {
return new String[] { "com.skcraft.alicefixes.TransformMiningLaser",
"com.skcraft.alicefixes.TransformBlockBreaker"
/*"com.skcraft.alicefixes.TransformIC2Explosions",
"com.skcraft.alicefixes.TransformTCExcWand",
"com.skcraft.alicefixes.TransformTCEquWand",
"com.skcraft.alicefixes.TransformTCFrostWand",
"com.skcraft.alicefixes.TransformTCAxe",
"com.skcraft.alicefixes.TransformTCShovel"*/};
}

}
9 changes: 5 additions & 4 deletions alicefixes/src/com/skcraft/alicefixes/ObfNames.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.skcraft.alicefixes;

public class ObfNames {

public static final String ENTITY_LIVING = "md";
public static final String ENTITY = "lq";


public static final String ENTITY_LIVING = "md";
public static final String ENTITY = "lq";
public static final String TILE_ENTITY = "any";

}
86 changes: 86 additions & 0 deletions alicefixes/src/com/skcraft/alicefixes/TransformBlockBreaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.skcraft.alicefixes;

import static org.objectweb.asm.Opcodes.*;

import java.util.Iterator;
import java.util.logging.Level;

import net.minecraft.tileentity.TileEntity;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;

import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.IClassTransformer;

public class TransformBlockBreaker implements IClassTransformer {

@Override
public byte[] transform(String name, byte[] bytes) {
if(name.equals("com.eloraam.redpower.machine.TileBreaker")) {
return handleBreakerTransform(bytes);
}
return bytes;
}

private byte[] handleBreakerTransform(byte[] bytes) {

ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(bytes);
classReader.accept(classNode, 0);

Iterator<MethodNode> methods = classNode.methods.iterator();
while(methods.hasNext()) {
MethodNode method = methods.next();
if(method.name.equals("onBlockNeighborChange")) {
LabelNode l0 = new LabelNode();
LabelNode l1 = new LabelNode();
LabelNode l2 = new LabelNode();
InsnList toInject = new InsnList();
toInject.add(l0);
toInject.add(new VarInsnNode(ALOAD, 0)); //this
toInject.add(new VarInsnNode(ALOAD, 0)); //this
toInject.add(new FieldInsnNode(GETFIELD, "com/eloraam/redpower/machine/TileBreaker", "Rotation", "I"));
toInject.add(new MethodInsnNode(INVOKESTATIC,
"com/skcraft/alicefixes/TransformBlockBreaker",
"canMine",
"(L" + ObfNames.TILE_ENTITY + ";I)Z")); //Invoke canMine() in this class
toInject.add(new JumpInsnNode(IFNE, l1)); //if statement
toInject.add(l2);
toInject.add(new InsnNode(RETURN));
toInject.add(l1);

method.instructions.insertBefore(method.instructions.getFirst(), toInject); //insert before first instruction
FMLLog.log(Level.INFO, "Block Breaker successfully patched!");
break;
}
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}

public static boolean canMine(TileEntity tile, int rotation) {

CoordHelper facingCoords = new CoordHelper(tile.xCoord, tile.yCoord, tile.zCoord);
facingCoords.addFacingAsOffset(rotation);

int id = tile.worldObj.getBlockId(facingCoords.x, facingCoords.y, facingCoords.z);

for(int i = 0; i < BreakerBlacklist.forbiddenIds.length; i++) {
if(id == BreakerBlacklist.forbiddenIds[i]) {
return false;
}
}
return true;
}
}
Loading

0 comments on commit 083453b

Please sign in to comment.