Search in sources :

Example 1 with GameRules

use of net.minecraft.world.GameRules in project BetterStorage by copygirl.

the class EntityFrienderman method onLivingUpdate.

@Override
public void onLivingUpdate() {
    if (worldObj.isRemote) {
        super.onLivingUpdate();
        return;
    }
    int x = (int) Math.floor(posX);
    int y = (int) (posY + 0.6);
    int z = (int) Math.floor(posZ);
    GameRules rules = worldObj.getGameRules();
    String ruleBefore = rules.getGameRuleStringValue("mobGriefing");
    boolean ruleChanged = false;
    boolean hasEnderChest = (func_146080_bZ() == Blocks.ender_chest);
    boolean hasBackpack = (getEquipmentInSlot(EquipmentSlot.CHEST) != null);
    // Temporarily change the blocks which endermen can carry.
    // TODO: Make a pull request to Forge which allows us to do this by overriding a method instead.
    IdentityHashMap<Block, Boolean> carriable = ReflectionUtils.get(EntityEnderman.class, null, "carriable", "");
    ReflectionUtils.set(EntityEnderman.class, null, "carriable", "", friendermanCarriable);
    // prevent dropping of enderchests regardless of the gamerule.
    if (hasEnderChest) {
        if (ruleBefore.equalsIgnoreCase("true")) {
            rules.setOrCreateGameRule("mobGriefing", "false");
            ruleChanged = true;
        }
    } else if (hasBackpack && worldObj.isAirBlock(x, y, z)) {
        if (ruleBefore.equalsIgnoreCase("false")) {
            rules.setOrCreateGameRule("mobGriefing", "true");
            ruleChanged = true;
        }
    }
    super.onLivingUpdate();
    // Reset carriable blocks and gamerule.
    ReflectionUtils.set(EntityEnderman.class, null, "carriable", "", carriable);
    if (ruleChanged)
        rules.setOrCreateGameRule("mobGriefing", ruleBefore);
    // If ender chest was picked up, remove ender backpack and place it on the ground.
    if (!hasEnderChest && (func_146080_bZ() == Blocks.ender_chest)) {
        setCurrentItemOrArmor(3, null);
        worldObj.setBlock(x, y, z, BetterStorageTiles.enderBackpack, RandomUtils.getInt(2, 6), 3);
        WorldUtils.get(worldObj, x, y, z, TileEntityBackpack.class).stack = new ItemStack(BetterStorageItems.itemEnderBackpack);
        double px = x + 0.5;
        double py = y + 0.5;
        double pz = z + 0.5;
        BetterStorage.networkChannel.sendToAllAround(new PacketBackpackTeleport(px, py, pz, x, y, z), worldObj, px, py, pz, 256);
        worldObj.playSoundEffect(px, py, pz, "mob.endermen.portal", 1.0F, 1.0F);
    }
}
Also used : PacketBackpackTeleport(net.mcft.copy.betterstorage.network.packet.PacketBackpackTeleport) GameRules(net.minecraft.world.GameRules) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Aggregations

PacketBackpackTeleport (net.mcft.copy.betterstorage.network.packet.PacketBackpackTeleport)1 Block (net.minecraft.block.Block)1 ItemStack (net.minecraft.item.ItemStack)1 GameRules (net.minecraft.world.GameRules)1