Last active
April 29, 2018 04:50
-
-
Save primetoxinz/c4bdc5187af58b0bc50a5b789f5b6d06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.primetoxinz; | |
import net.minecraft.block.BlockLog; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.entity.player.EntityPlayerMP; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.item.crafting.Ingredient; | |
import net.minecraft.util.ResourceLocation; | |
import net.minecraft.util.math.BlockPos; | |
import net.minecraft.world.World; | |
import net.minecraftforge.common.config.Config; | |
import net.minecraftforge.common.config.ConfigManager; | |
import net.minecraftforge.event.world.BlockEvent; | |
import net.minecraftforge.fml.client.event.ConfigChangedEvent; | |
import net.minecraftforge.fml.common.Mod; | |
import net.minecraftforge.fml.common.Mod.EventHandler; | |
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; | |
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
import net.minecraftforge.fml.common.registry.ForgeRegistries; | |
import net.minecraftforge.oredict.OreDictionary; | |
import org.apache.logging.log4j.Logger; | |
import java.util.Arrays; | |
import java.util.LinkedHashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
@Mod.EventBusSubscriber | |
@Mod(modid = BWW.MODID, name = BWW.NAME, version = BWW.VERSION) | |
public class BWW { | |
public static final String MODID = "betterwithwood"; | |
public static final String NAME = "Better With Wood"; | |
public static final String VERSION = "1.0"; | |
private static Logger LOGGER; | |
private static List<Ingredient> BLACKLIST; | |
public static boolean isLog(IBlockState state) { | |
return state.getBlock() instanceof BlockLog; | |
} | |
public static boolean isLog(World world, BlockPos pos) { | |
return isLog(world.getBlockState(pos)); | |
} | |
@SubscribeEvent | |
public static void onBlockHarvest(BlockEvent.HarvestDropsEvent event) { | |
if (event.getWorld().isRemote) | |
return; | |
if (event.getHarvester() == null || !event.getHarvester().isSneaking()) | |
return; | |
if (!Configuration.canEmptyHandChop && areHandsEmpty(event.getHarvester())) | |
return; | |
if (isHoldingBlacklist(event.getHarvester())) | |
return; | |
if (!isLog(event.getState())) | |
return; | |
Set<BlockPos> queue = new LinkedHashSet<>(); | |
for (int x = -1; x <= 1; x++) { | |
for (int z = -1; z <= 1; z++) { | |
for (int y = 0; y <= 1; y++) { | |
BlockPos p = event.getPos().add(x, y, z); | |
if (isLog(event.getWorld(), p)) | |
queue.add(p); | |
} | |
} | |
} | |
for (BlockPos pos : queue) { | |
harvestBlock(event.getWorld(), pos, (EntityPlayerMP) event.getHarvester()); | |
} | |
} | |
public static void harvestBlock(World world, BlockPos pos, EntityPlayerMP player) { | |
if (world.isAirBlock(pos) || world.getTileEntity(pos) != null) | |
return; | |
player.interactionManager.tryHarvestBlock(pos); | |
} | |
@SubscribeEvent | |
public static void onConfigChange(ConfigChangedEvent event) { | |
if (event.getModID().equals(MODID)) { | |
ConfigManager.sync(MODID, Config.Type.INSTANCE); | |
BLACKLIST = null; | |
} | |
} | |
private static Ingredient stackFromString(String name) { | |
String[] split = name.split(":"); | |
if (split.length > 1) { | |
int meta = OreDictionary.WILDCARD_VALUE; | |
if (split.length > 2) { | |
meta = Integer.parseInt(split[2]); | |
} | |
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(split[0], split[1])); | |
if (item != null) { | |
return Ingredient.fromStacks(new ItemStack(item, 1, meta)); | |
} | |
} | |
return Ingredient.EMPTY; | |
} | |
private static List<Ingredient> loadItemStackList(String[] array) { | |
return Arrays.stream(array).map(BWW::stackFromString).collect(Collectors.toList()); | |
} | |
private static boolean areHandsEmpty(EntityPlayer player) { | |
ItemStack main = player.getHeldItemMainhand(); | |
ItemStack secondary = player.getHeldItemOffhand(); | |
return main.isEmpty() && secondary.isEmpty(); | |
} | |
private static boolean isHoldingBlacklist(EntityPlayer player) { | |
ItemStack main = player.getHeldItemMainhand(); | |
ItemStack secondary = player.getHeldItemOffhand(); | |
boolean a = contains(getBlacklist(), main), b = contains(getBlacklist(), secondary); | |
return a || b; | |
} | |
private static boolean contains(List<Ingredient> list, ItemStack stack) { | |
return list.stream().anyMatch(i -> i.apply(stack)); | |
} | |
private static List<Ingredient> getBlacklist() { | |
if (BLACKLIST == null) | |
BLACKLIST = loadItemStackList(Configuration.itemBlacklist); | |
return BLACKLIST; | |
} | |
@EventHandler | |
public void preInit(FMLPreInitializationEvent event) { | |
LOGGER = event.getModLog(); | |
} | |
@Config(modid = BWW.MODID) | |
public static class Configuration { | |
@Config.Name("Can empty hand chop") | |
public static boolean canEmptyHandChop = true; | |
@Config.Name("Item BLACKLIST") | |
@Config.Comment("Blacklist for the tools that can cause a tree to be chopped") | |
public static String[] itemBlacklist = new String[]{"minecraft:stone_axe"}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment