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

195 lines
9.4 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;
2021-11-14 19:18:07 +01:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
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;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.items.ItemBag;
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
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.ItemStackHandlerAA;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.monster.SpiderEntity;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
2016-07-03 20:57:00 +02:00
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
2017-02-13 15:34:35 +01:00
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
2017-06-17 00:55:55 +02:00
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;
2016-07-03 20:57:00 +02:00
2021-02-26 22:15:48 +01:00
import java.util.Locale;
2019-05-02 09:10:29 +02:00
public class CommonEvents {
2016-07-03 20:57:00 +02:00
2017-06-17 00:55:55 +02:00
@SubscribeEvent
public void onBlockBreakEvent(BlockEvent.BreakEvent event) {
2021-02-26 22:15:48 +01:00
BlockState state = event.getState();
if (state != null && state.getBlock() == Blocks.SPAWNER) {
// TODO: [port] add back once we've unflattened
// event.getDrops().add(new ItemStack(InitItems.itemMisc, 1, TheMiscItems.SPAWNER_SHARD.ordinal()));
2017-06-17 00:55:55 +02:00
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onItemPickup(EntityItemPickupEvent event) {
2021-02-26 22:15:48 +01:00
if (event.isCanceled() || event.getResult() == Event.Result.ALLOW) {
return;
}
2017-06-17 00:55:55 +02:00
PlayerEntity player = event.getPlayer();
ItemEntity item = event.getItem();
if (item != null && item.isAlive()) {
ItemStack stack = item.getItem();
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack)) {
for (int i = 0; i < player.inventory.getContainerSize(); i++) {
if (i != player.inventory.selected) {
2017-06-17 00:55:55 +02:00
ItemStack invStack = player.inventory.getItem(i);
if (StackUtil.isValid(invStack) && invStack.getItem() instanceof ItemBag && invStack.hasTag()) {
if (invStack.getOrCreateTag().getBoolean("AutoInsert")) {
2017-06-17 00:55:55 +02:00
boolean changed = false;
2019-05-02 09:10:29 +02:00
boolean isVoid = ((ItemBag) invStack.getItem()).isVoid;
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerBag.getSlotAmount(isVoid));
2017-06-17 00:55:55 +02:00
ItemDrill.loadSlotsFromNBT(inv, invStack);
FilterSettings filter = new FilterSettings(4, false, false, false, false, 0, 0);
filter.readFromNBT(invStack.getOrCreateTag(), "Filter");
2019-05-02 09:10:29 +02:00
if (filter.check(stack)) {
if (isVoid) {
stack.setCount(0);
2017-06-17 00:55:55 +02:00
changed = true;
2019-05-02 09:10:29 +02:00
} else {
for (int j = 0; j < inv.getSlots(); j++) {
2017-06-17 00:55:55 +02:00
ItemStack bagStack = inv.getStackInSlot(j);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(bagStack)) {
if (ItemUtil.canBeStacked(bagStack, stack)) {
int maxTransfer = Math.min(stack.getCount(), stack.getMaxStackSize() - bagStack.getCount());
if (maxTransfer > 0) {
inv.setStackInSlot(j, StackUtil.grow(bagStack, maxTransfer));
stack.shrink(maxTransfer);
2017-06-17 00:55:55 +02:00
changed = true;
}
}
2019-05-02 09:10:29 +02:00
} else {
2017-06-17 00:55:55 +02:00
inv.setStackInSlot(j, stack.copy());
stack.setCount(0);
2017-06-17 00:55:55 +02:00
changed = true;
}
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(stack)) {
2017-06-17 00:55:55 +02:00
break;
}
}
}
}
2019-05-02 09:10:29 +02:00
if (changed) {
if (!isVoid) {
2017-06-17 00:55:55 +02:00
ItemDrill.writeSlotsToNBT(inv, invStack);
}
event.setResult(Event.Result.ALLOW);
}
}
}
}
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(stack)) {
2017-06-17 00:55:55 +02:00
break;
}
}
}
item.setItem(stack);
2017-06-17 00:55:55 +02:00
}
}
2017-06-17 00:48:49 +02:00
//TODO Checking Achievements?
2021-02-26 22:15:48 +01:00
/*public static void checkAchievements(ItemStack gotten, PlayerEntity 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
2019-05-02 09:10:29 +02:00
public void onEntityDropEvent(LivingDropsEvent event) {
if (event.getEntityLiving().level != null && !event.getEntityLiving().level.isClientSide && event.getSource().getEntity() instanceof PlayerEntity) {
2016-07-03 20:57:00 +02:00
//Drop Cobwebs from Spiders
if (ConfigBoolValues.DO_SPIDER_DROPS.isEnabled() && event.getEntityLiving() instanceof SpiderEntity) {
if (event.getEntityLiving().level.random.nextInt(20) <= event.getLootingLevel() * 2) {
event.getDrops().add(new ItemEntity(event.getEntityLiving().level, event.getEntityLiving().getX(), event.getEntityLiving().getY(), event.getEntityLiving().getZ(), new ItemStack(Blocks.COBWEB, event.getEntityLiving().level.random.nextInt(2 + event.getLootingLevel()) + 1)));
2016-07-03 20:57:00 +02:00
}
}
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event) {
if (!event.getPlayer().level.isClientSide && event.getPlayer() instanceof ServerPlayerEntity) {
ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
PacketHandlerHelper.syncPlayerData(player, true);
ActuallyAdditions.LOGGER.info("Sending Player Data to player " + player.getName() + " with UUID " + player.getUUID() + ".");
2016-07-03 20:57:00 +02:00
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
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
2021-11-14 19:18:07 +01:00
if (CommonConfig.OTHER.GIVE_BOOKLET_ON_FIRST_CRAFT.get()) {
if (!event.getPlayer().level.isClientSide && StackUtil.isValid(event.getCrafting()) && event.getCrafting().getItem() != ActuallyItems.ITEM_BOOKLET.get()) {
2016-07-03 20:57:00 +02:00
String name = event.getCrafting().getItem().getRegistryName().toString();
2019-05-02 09:10:29 +02:00
if (name != null && name.toLowerCase(Locale.ROOT).contains(ActuallyAdditions.MODID)) {
PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.getPlayer());
2019-05-02 09:10:29 +02:00
if (save != null && !save.bookGottenAlready) {
save.bookGottenAlready = true;
WorldData.get(event.getPlayer().getCommandSenderWorld()).setDirty();
2016-07-03 20:57:00 +02:00
ItemEntity entityItem = new ItemEntity(event.getPlayer().level, event.getPlayer().getX(), event.getPlayer().getY(), event.getPlayer().getZ(), new ItemStack(ActuallyItems.ITEM_BOOKLET.get()));
entityItem.setPickUpDelay(0);
event.getPlayer().level.addFreshEntity(entityItem);
2016-07-03 20:57:00 +02:00
}
}
}
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
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
2019-05-02 09:10:29 +02:00
public void onPickupEvent(EntityItemPickupEvent event) {
//checkAchievements(event.getItem().getItem(), event.getEntityPlayer(), InitAchievements.Type.PICK_UP);
2016-07-03 20:57:00 +02:00
}
}