ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java

209 lines
9.8 KiB
Java
Raw Normal View History

2016-07-03 20:57:00 +02:00
/*
* This file ("CommonEvents.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-07-03 20:57:00 +02:00
*/
package de.ellpeck.actuallyadditions.mod.event;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.inventory.ContainerBag;
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.items.InitItems;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.items.ItemBag;
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems;
2016-10-30 20:28:02 +01:00
import de.ellpeck.actuallyadditions.mod.misc.DungeonLoot;
2016-08-09 20:56:09 +02:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.tile.FilterSettings;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerCustom;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2017-06-17 00:55:55 +02:00
import net.minecraft.block.state.IBlockState;
2016-07-03 20:57:00 +02:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
2016-07-03 22:53:12 +02:00
import net.minecraftforge.common.MinecraftForge;
2016-07-03 20:57:00 +02:00
import net.minecraftforge.event.entity.living.LivingDropsEvent;
2017-02-13 15:34:35 +01:00
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
2017-06-17 00:55:55 +02:00
import net.minecraftforge.event.world.BlockEvent;
2016-07-03 20:57:00 +02:00
import net.minecraftforge.event.world.WorldEvent;
2017-06-17 00:55:55 +02:00
import net.minecraftforge.fml.common.eventhandler.Event;
2016-07-03 20:57:00 +02:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import java.util.Locale;
public class CommonEvents{
2016-07-03 22:53:12 +02:00
public CommonEvents(){
MinecraftForge.EVENT_BUS.register(this);
2016-10-30 20:28:02 +01:00
MinecraftForge.EVENT_BUS.register(new DungeonLoot());
2016-07-03 22:53:12 +02:00
}
2017-06-17 00:55:55 +02:00
@SubscribeEvent
public void onBlockBreakEvent(BlockEvent.HarvestDropsEvent event){
IBlockState state = event.getState();
if(state != null && state.getBlock() == Blocks.MOB_SPAWNER){
event.getDrops().add(new ItemStack(InitItems.itemMisc, 1, TheMiscItems.SPAWNER_SHARD.ordinal()));
}
}
@SubscribeEvent
public void onItemPickup(EntityItemPickupEvent event){
if(event.isCanceled() || event.getResult() == Event.Result.ALLOW){
return;
}
EntityPlayer player = event.getEntityPlayer();
EntityItem item = event.getItem();
if(item != null && !item.isDead){
ItemStack stack = item.getItem();
2017-06-17 00:55:55 +02:00
if(StackUtil.isValid(stack)){
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
if(i != player.inventory.currentItem){
ItemStack invStack = player.inventory.getStackInSlot(i);
if(StackUtil.isValid(invStack) && invStack.getItem() instanceof ItemBag && invStack.hasTagCompound()){
if(invStack.getTagCompound().getBoolean("AutoInsert")){
boolean changed = false;
boolean isVoid = ((ItemBag)invStack.getItem()).isVoid;
ItemStackHandlerCustom inv = new ItemStackHandlerCustom(ContainerBag.getSlotAmount(isVoid));
ItemDrill.loadSlotsFromNBT(inv, invStack);
FilterSettings filter = new FilterSettings(4, false, false, false, false, 0, 0);
filter.readFromNBT(invStack.getTagCompound(), "Filter");
if(filter.check(stack)){
if(isVoid){
stack = StackUtil.setStackSize(stack, 0);
changed = true;
}
else{
for(int j = 0; j < inv.getSlots(); j++){
ItemStack bagStack = inv.getStackInSlot(j);
if(StackUtil.isValid(bagStack)){
if(ItemUtil.canBeStacked(bagStack, stack)){
int maxTransfer = Math.min(StackUtil.getStackSize(stack), stack.getMaxStackSize()-StackUtil.getStackSize(bagStack));
if(maxTransfer > 0){
inv.setStackInSlot(j, StackUtil.addStackSize(bagStack, maxTransfer));
stack = StackUtil.addStackSize(stack, -maxTransfer);
changed = true;
}
}
}
else{
inv.setStackInSlot(j, stack.copy());
stack = StackUtil.setStackSize(stack, 0);
changed = true;
}
if(!StackUtil.isValid(stack)){
break;
}
}
}
}
if(changed){
if(!isVoid){
ItemDrill.writeSlotsToNBT(inv, invStack);
}
event.setResult(Event.Result.ALLOW);
}
}
}
}
if(!StackUtil.isValid(stack)){
break;
}
}
}
item.setItem(stack);
2017-06-17 00:55:55 +02:00
}
}
2017-06-17 00:48:49 +02:00
//TODO Checking Achievements?
/*public static void checkAchievements(ItemStack gotten, EntityPlayer player, InitAchievements.Type type){
2017-02-13 15:34:35 +01:00
if(gotten != null && player != null){
2016-08-05 04:49:04 +02:00
for(TheAchievements ach : TheAchievements.values()){
if(ach.type == type){
if(ItemUtil.contains(ach.itemsToBeGotten, gotten, true)){
ach.get(player);
2016-07-07 17:59:45 +02:00
}
}
}
}
2017-06-17 00:48:49 +02:00
}*/
2016-07-07 17:59:45 +02:00
2016-07-03 20:57:00 +02:00
@SubscribeEvent
public void onEntityDropEvent(LivingDropsEvent event){
if(event.getEntityLiving().world != null && !event.getEntityLiving().world.isRemote && event.getSource().getTrueSource() instanceof EntityPlayer){
2016-07-03 20:57:00 +02:00
//Drop Cobwebs from Spiders
if(ConfigBoolValues.DO_SPIDER_DROPS.isEnabled() && event.getEntityLiving() instanceof EntitySpider){
2016-11-26 21:32:27 +01:00
if(event.getEntityLiving().world.rand.nextInt(20) <= event.getLootingLevel()*2){
2016-12-11 20:52:47 +01:00
event.getDrops().add(new EntityItem(event.getEntityLiving().world, event.getEntityLiving().posX, event.getEntityLiving().posY, event.getEntityLiving().posZ, new ItemStack(Blocks.WEB, event.getEntityLiving().world.rand.nextInt(2+event.getLootingLevel())+1)));
2016-07-03 20:57:00 +02:00
}
}
}
}
@SubscribeEvent
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){
2016-11-26 21:32:27 +01:00
if(!event.player.world.isRemote && event.player instanceof EntityPlayerMP){
EntityPlayerMP player = (EntityPlayerMP)event.player;
PacketHandlerHelper.syncPlayerData(player, true);
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.info("Sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+".");
2016-07-03 20:57:00 +02:00
}
}
@SubscribeEvent
public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event){
2017-06-17 00:48:49 +02:00
//checkAchievements(event.crafting, event.player, InitAchievements.Type.CRAFTING);
2016-07-03 20:57:00 +02:00
if(ConfigBoolValues.GIVE_BOOKLET_ON_FIRST_CRAFT.isEnabled()){
2016-11-26 21:32:27 +01:00
if(!event.player.world.isRemote && StackUtil.isValid(event.crafting) && event.crafting.getItem() != InitItems.itemBooklet){
2016-07-03 20:57:00 +02:00
String name = event.crafting.getItem().getRegistryName().toString();
2018-05-10 11:38:58 +02:00
if(name != null && name.toLowerCase(Locale.ROOT).contains(ActuallyAdditions.MODID)){
PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.player);
if(save != null && !save.bookGottenAlready){
save.bookGottenAlready = true;
WorldData.get(event.player.getEntityWorld()).markDirty();
2016-07-03 20:57:00 +02:00
2016-11-26 21:32:27 +01:00
EntityItem entityItem = new EntityItem(event.player.world, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemBooklet));
2016-07-03 20:57:00 +02:00
entityItem.setPickupDelay(0);
2016-11-26 21:32:27 +01:00
event.player.world.spawnEntity(entityItem);
2016-07-03 20:57:00 +02:00
}
}
}
}
}
@SubscribeEvent
public void onSmeltedEvent(PlayerEvent.ItemSmeltedEvent event){
2017-06-17 00:48:49 +02:00
//checkAchievements(event.smelting, event.player, InitAchievements.Type.SMELTING);
2016-07-03 20:57:00 +02:00
}
@SubscribeEvent
2017-02-13 15:34:35 +01:00
public void onPickupEvent(EntityItemPickupEvent event){
//checkAchievements(event.getItem().getItem(), event.getEntityPlayer(), InitAchievements.Type.PICK_UP);
2016-07-03 20:57:00 +02:00
}
@SubscribeEvent
public void onLoad(WorldEvent.Load event){
WorldData.loadLegacy(event.getWorld());
2016-07-03 20:57:00 +02:00
}
}