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;
|
2021-03-01 21:23:52 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
|
2021-11-25 21:27:45 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.DrillItem;
|
2017-06-17 00:55:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemBag;
|
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;
|
2018-06-23 01:39:30 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
|
2017-06-17 00:55:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2021-02-27 17:35:21 +01:00
|
|
|
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;
|
2021-02-27 17:35:21 +01:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
2017-06-17 00:55:55 +02:00
|
|
|
import net.minecraftforge.event.world.BlockEvent;
|
2021-02-27 17:35:21 +01:00
|
|
|
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
|
2021-02-27 17:35:21 +01:00
|
|
|
public void onBlockBreakEvent(BlockEvent.BreakEvent event) {
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState state = event.getState();
|
2021-02-27 17:35:21 +01:00
|
|
|
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
|
|
|
|
2021-02-27 17:35:21 +01:00
|
|
|
PlayerEntity player = event.getPlayer();
|
|
|
|
ItemEntity item = event.getItem();
|
|
|
|
if (item != null && item.isAlive()) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack stack = item.getItem();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack)) {
|
2021-08-22 17:09:06 +02:00
|
|
|
for (int i = 0; i < player.inventory.getContainerSize(); i++) {
|
|
|
|
if (i != player.inventory.selected) {
|
2017-06-17 00:55:55 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
ItemStack invStack = player.inventory.getItem(i);
|
2021-02-27 17:35:21 +01:00
|
|
|
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;
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerBag.getSlotAmount(isVoid));
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.loadSlotsFromNBT(inv, invStack);
|
2017-06-17 00:55:55 +02:00
|
|
|
|
|
|
|
FilterSettings filter = new FilterSettings(4, false, false, false, false, 0, 0);
|
2021-02-27 17:35:21 +01:00
|
|
|
filter.readFromNBT(invStack.getOrCreateTag(), "Filter");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (filter.check(stack)) {
|
|
|
|
if (isVoid) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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());
|
2018-06-23 01:39:30 +02:00
|
|
|
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) {
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.writeSlotsToNBT(inv, invStack);
|
2017-06-17 00:55:55 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:30:02 +02:00
|
|
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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
|
2021-02-27 17:35:21 +01:00
|
|
|
if (ConfigBoolValues.DO_SPIDER_DROPS.isEnabled() && event.getEntityLiving() instanceof SpiderEntity) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!event.getPlayer().level.isClientSide && event.getPlayer() instanceof ServerPlayerEntity) {
|
2021-02-27 17:35:21 +01:00
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
|
2017-02-13 15:23:28 +01:00
|
|
|
PacketHandlerHelper.syncPlayerData(player, true);
|
2021-08-22 17:09:06 +02:00
|
|
|
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()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!event.getPlayer().level.isClientSide && StackUtil.isValid(event.getCrafting()) && event.getCrafting().getItem() != ActuallyItems.ITEM_BOOKLET.get()) {
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-02-27 17:35:21 +01: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)) {
|
2021-02-27 17:35:21 +01:00
|
|
|
PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.getPlayer());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (save != null && !save.bookGottenAlready) {
|
2016-11-11 21:41:07 +01:00
|
|
|
save.bookGottenAlready = true;
|
2021-08-22 17:09:06 +02:00
|
|
|
WorldData.get(event.getPlayer().getCommandSenderWorld()).setDirty();
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-08-22 17:09:06 +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) {
|
2017-06-29 18:30:02 +02:00
|
|
|
//checkAchievements(event.getItem().getItem(), event.getEntityPlayer(), InitAchievements.Type.PICK_UP);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|