Merge branch 'master' of github.com:MightyPirates/OpenComponents into MC1.7

Conflicts:
	src/main/java/li/cil/occ/mods/vanilla/DriverInventory.java
This commit is contained in:
Florian Nücke 2014-10-03 13:19:59 +02:00
commit b0d9c8d056
2 changed files with 45 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package li.cil.occ;
import com.mojang.authlib.GameProfile;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
@ -25,6 +26,8 @@ import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.UUID;
@Mod(modid = OpenComponents.ID, name = OpenComponents.Name, version = OpenComponents.Version, useMetadata = true)
public class OpenComponents {
public static final String ID = "OpenComponents";
@ -48,6 +51,12 @@ public class OpenComponents {
public static Boolean allowItemStackInspection = false;
public static String fakePlayerUuid = "7e506b5d-2ccb-4ac4-a249-5624925b0c67";
public static String fakePlayerName = "[OpenComponents]";
public static GameProfile fakePlayerProfile;
@Mod.EventHandler
public void preInit(final FMLPreInitializationEvent e) {
final Configuration config = new Configuration(e.getSuggestedConfigurationFile());
@ -68,6 +77,14 @@ public class OpenComponents {
allowItemStackInspection = config.get("vanilla", "allowItemStackInspection", false).
getBoolean(false);
fakePlayerUuid = config.get("general", "fakePlayerUuid", fakePlayerUuid).
getString();
fakePlayerName = config.get("general", "fakePlayerName", fakePlayerName).
getString();
fakePlayerProfile = new GameProfile(UUID.fromString(fakePlayerUuid), fakePlayerName);
config.save();
}

View File

@ -7,9 +7,14 @@ import li.cil.oc.api.network.ManagedEnvironment;
import li.cil.oc.api.prefab.DriverTileEntity;
import li.cil.occ.OpenComponents;
import li.cil.occ.mods.ManagedTileEntityEnvironment;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;
public final class DriverInventory extends DriverTileEntity {
@Override
@ -19,26 +24,34 @@ public final class DriverInventory extends DriverTileEntity {
@Override
public ManagedEnvironment createEnvironment(final World world, final int x, final int y, final int z) {
return new Environment((IInventory) world.getTileEntity(x, y, z));
return new Environment(world.getTileEntity(x, y, z), world);
}
public static final class Environment extends ManagedTileEntityEnvironment<IInventory> {
public Environment(final IInventory tileEntity) {
super(tileEntity, "inventory");
private final EntityPlayer fakePlayer;
private final Vec3 position;
public Environment(final TileEntity tileEntity, final World world) {
super((IInventory) tileEntity, "inventory");
fakePlayer = FakePlayerFactory.get((WorldServer) world, OpenComponents.fakePlayerProfile);
position = Vec3.createVectorHelper(tileEntity.xCoord + 0.5, tileEntity.yCoord + 0.5, tileEntity.zCoord + 0.5);
}
@Callback(doc = "function():string -- Get the name of this inventory.")
public Object[] getInventoryName(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
return new Object[]{tileEntity.getInventoryName()};
}
@Callback(doc = "function():number -- Get the number of slots in this inventory.")
public Object[] getInventorySize(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
return new Object[]{tileEntity.getSizeInventory()};
}
@Callback(doc = "function(slot:number):number -- Get the stack size of the item stack in the specified slot.")
public Object[] getSlotStackSize(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
final int slot = checkSlot(args, 0);
final ItemStack stack = tileEntity.getStackInSlot(slot);
if (stack != null) {
@ -50,6 +63,7 @@ public final class DriverInventory extends DriverTileEntity {
@Callback(doc = "function(slot:number):number -- Get the maximum stack size of the item stack in the specified slot.")
public Object[] getSlotMaxStackSize(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
final int slot = checkSlot(args, 0);
final ItemStack stack = tileEntity.getStackInSlot(slot);
if (stack != null) {
@ -61,6 +75,7 @@ public final class DriverInventory extends DriverTileEntity {
@Callback(doc = "function(slotA:number, slotB:number):boolean -- Compare the two item stacks in the specified slots for equality.")
public Object[] compareStacks(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
final int slotA = checkSlot(args, 0);
final int slotB = checkSlot(args, 1);
if (slotA == slotB) {
@ -79,6 +94,7 @@ public final class DriverInventory extends DriverTileEntity {
@Callback(doc = "function(slotA:number, slotB:number[, count:number=math.huge]):boolean -- Move up to the specified number of items from the first specified slot to the second.")
public Object[] transferStack(final Context context, final Arguments args) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
final int slotA = checkSlot(args, 0);
final int slotB = checkSlot(args, 1);
final int count = Math.max(0, Math.min(args.count() > 2 && args.checkAny(2) != null ? args.checkInteger(2) : 64, tileEntity.getInventoryStackLimit()));
@ -121,6 +137,7 @@ public final class DriverInventory extends DriverTileEntity {
@Callback(doc = "function(slot:number):table -- Get a description of the item stack in the specified slot.")
public Object[] getStackInSlot(final Context context, final Arguments args) {
if (OpenComponents.allowItemStackInspection) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
return new Object[]{tileEntity.getStackInSlot(checkSlot(args, 0))};
} else {
return new Object[]{null, "not enabled in config"};
@ -130,6 +147,7 @@ public final class DriverInventory extends DriverTileEntity {
@Callback(doc = "function():table -- Get a list of descriptions for all item stacks in this inventory.")
public Object[] getAllStacks(final Context context, final Arguments args) {
if (OpenComponents.allowItemStackInspection) {
if (!checkPermission()) return new Object[]{null, "permission denied"};
ItemStack[] allStacks = new ItemStack[tileEntity.getSizeInventory()];
for (int i = 0; i < tileEntity.getSizeInventory(); i++) {
allStacks[i] = tileEntity.getStackInSlot(i);
@ -151,5 +169,12 @@ public final class DriverInventory extends DriverTileEntity {
private boolean itemEquals(final ItemStack stackA, final ItemStack stackB) {
return stackA.getItem().equals(stackB.getItem()) && !stackA.getHasSubtypes() || stackA.getItemDamage() == stackB.getItemDamage();
}
private boolean checkPermission() {
synchronized (fakePlayer) {
fakePlayer.setPosition(position.xCoord, position.yCoord, position.zCoord);
return tileEntity.isUseableByPlayer(fakePlayer);
}
}
}
}