Fixed the bag of holding

This commit is contained in:
Sollace 2020-05-07 13:16:55 +02:00
parent 25352710b7
commit 51f9625d2b
9 changed files with 106 additions and 50 deletions

View file

@ -7,6 +7,8 @@ import com.minelittlepony.unicopia.ability.data.Hit;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.magic.spell.ChangelingTrapSpell;
// TODO: Make this a throwable item instead
@Deprecated
public class ChangelingTrapAbility implements Ability<Hit> {
@Override

View file

@ -1,6 +1,7 @@
package com.minelittlepony.unicopia.client;
import com.minelittlepony.unicopia.block.UBlocks;
import com.minelittlepony.unicopia.client.gui.UScreens;
import com.minelittlepony.unicopia.client.particle.ChangelingMagicParticle;
import com.minelittlepony.unicopia.client.particle.DiskParticle;
import com.minelittlepony.unicopia.client.particle.MagicParticle;
@ -51,6 +52,8 @@ public interface URenderers {
UBlocks.SLIME_DROP, UBlocks.SLIME_LAYER
);
UScreens.bootstrap();
}
}

View file

@ -1,13 +1,14 @@
package com.minelittlepony.unicopia.container;
package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.common.client.gui.element.Scrollbar;
import com.minelittlepony.unicopia.item.BagOfHoldingItem;
import com.minelittlepony.unicopia.container.BagOfHoldingContainer;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.gui.screen.ingame.ContainerScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.PacketByteBuf;
public class BagOfHoldingScreen extends ContainerScreen<BagOfHoldingContainer> {
private static final Identifier CHEST_GUI_TEXTURE = new Identifier("textures/gui/container/generic_54.png");
@ -17,12 +18,11 @@ public class BagOfHoldingScreen extends ContainerScreen<BagOfHoldingContainer> {
private final Scrollbar scrollbar = new Scrollbar();
public BagOfHoldingScreen(PlayerEntity player, BagOfHoldingItem.ContainerProvider provider) {
super(provider.createMenu(0, player.inventory, player), player.inventory, provider.getDisplayName());
public BagOfHoldingScreen(int sync, Identifier id, PlayerEntity player, PacketByteBuf buf) {
super(new BagOfHoldingContainer(sync, id, player, buf), player.inventory, buf.readText());
playerRows = playerInventory.getInvSize() / 9;
inventoryRows = (container.slots.size() / 9) - 1;
}
@Override
@ -70,6 +70,11 @@ public class BagOfHoldingScreen extends ContainerScreen<BagOfHoldingContainer> {
@Override
public boolean mouseDragged(double x, double y, int button, double dx, double dy) {
if (scrollbar.isMouseOver(x, y)) {
return scrollbar.mouseDragged(x, y + scrollbar.getScrollAmount(), button, dx, dy);
}
return super.mouseDragged(x, y + scrollbar.getScrollAmount(), button, dx, dy);
}

View file

@ -1,9 +1,10 @@
package com.minelittlepony.unicopia.container;
package com.minelittlepony.unicopia.client.gui;
import org.lwjgl.opengl.GL11;
import com.minelittlepony.common.client.gui.element.Button;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.container.SpellBookContainer;
import com.minelittlepony.unicopia.container.SpellBookContainer.SpellbookSlot;
import com.minelittlepony.unicopia.enchanting.IPageUnlockListener;
import com.minelittlepony.unicopia.enchanting.Page;

View file

@ -0,0 +1,12 @@
package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.unicopia.container.UContainers;
import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry;
public interface UScreens {
static void bootstrap() {
ScreenProviderRegistry.INSTANCE.registerFactory(UContainers.BAG_OF_HOLDING, BagOfHoldingScreen::new);
}
}

View file

@ -88,6 +88,8 @@ public class BagOfHoldingContainer extends Container {
}
}
sendContentUpdates();
return resultingStack;
}

View file

@ -15,11 +15,14 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.EnderChestBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.BasicInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.nbt.CompoundTag;
@ -90,6 +93,34 @@ public class BagOfHoldingInventory extends BasicInventory implements NbtSerialis
return name;
}
public void addEntity(World world, Entity entity) {
SpawnEggItem item = SpawnEggItem.forEntity(entity.getType());
ItemStack stack = new ItemStack(item);
CompoundTag tag = entity.toTag(stack.getOrCreateSubTag("EntityTag"));
tag.remove("Pos");
tag.remove("UUIDMost");
tag.remove("UUIDLeast");
for (ItemStack i : entity.getItemsEquipped()) {
if (isIllegalItem(i)) {
stack.getTag().putBoolean("invalid", true);
break;
}
}
add(stack);
entity.remove();
if (entity instanceof MobEntity) {
((MobEntity)entity).playAmbientSound();
}
world.playSound(null, entity.getBlockPos(), SoundEvents.UI_TOAST_IN, SoundCategory.PLAYERS, 3.5F, 0.25F);
}
public <T extends BlockEntity & Inventory> void addBlockEntity(World world, BlockPos pos, T blockInventory) {
BlockState state = world.getBlockState(pos);
@ -101,7 +132,7 @@ public class BagOfHoldingInventory extends BasicInventory implements NbtSerialis
ItemStack blockStack = state.getDroppedStacks(context).get(0);
blockInventory.toTag(blockStack.getSubTag("BlockEntityTag"));
blockInventory.toTag(blockStack.getOrCreateSubTag("BlockEntityTag"));
for (int i = 0; i < blockInventory.getInvSize(); i++) {
ItemStack stack = blockInventory.getInvStack(i);

View file

@ -12,7 +12,7 @@ public interface UContainers {
static Identifier register(String name, ContainerFactory<Container> factory) {
Identifier id = new Identifier("unicopia", name);
ContainerProviderRegistry.INSTANCE.registerFactory(BAG_OF_HOLDING, factory);
ContainerProviderRegistry.INSTANCE.registerFactory(id, factory);
return id;
}

View file

@ -6,9 +6,9 @@ import java.util.Map;
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.container.BagOfHoldingContainer;
import com.minelittlepony.unicopia.container.BagOfHoldingInventory;
import com.minelittlepony.unicopia.container.UContainers;
import com.minelittlepony.unicopia.entity.IMagicals;
import com.minelittlepony.unicopia.magic.Affinity;
import com.minelittlepony.unicopia.magic.MagicalItem;
import com.minelittlepony.unicopia.util.VecHelper;
@ -16,14 +16,14 @@ import com.minelittlepony.unicopia.util.VecHelper;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.container.NameableContainerFactory;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
@ -31,6 +31,7 @@ import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.BlockPos;
@ -55,9 +56,9 @@ public class BagOfHoldingItem extends Item implements MagicalItem {
return true;
});
for (Text name : counts.keySet()) {
tooltip.add(name.append(" ").append(counts.get(name).toString()));
}
counts.forEach((line, count) -> {
tooltip.add(line.deepCopy().append(" ").append(count.toString()));
});
}
@Override
@ -80,12 +81,13 @@ public class BagOfHoldingItem extends Item implements MagicalItem {
BlockEntity tile = world.getBlockEntity(pos);
if (tile instanceof Inventory) {
BagOfHoldingInventory inventory = BagOfHoldingInventory.getInventoryFromStack(stack);
inventory.addBlockEntity(world, pos, (BlockEntity & Inventory)tile);
inventory.writeTostack(stack);
inventory.onInvClose(player);
if (!world.isClient) {
BagOfHoldingInventory inventory = BagOfHoldingInventory.getInventoryFromStack(stack);
inventory.addBlockEntity(world, pos, (BlockEntity & Inventory)tile);
inventory.writeTostack(stack);
inventory.onInvClose(player);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
@ -94,12 +96,27 @@ public class BagOfHoldingItem extends Item implements MagicalItem {
List<Entity> itemsAround = world.getEntities(player, box, EquinePredicates.IS_VALID_ITEM);
if (itemsAround.size() > 0) {
BagOfHoldingInventory inventory = BagOfHoldingInventory.getInventoryFromStack(stack);
if (!world.isClient) {
BagOfHoldingInventory inventory = BagOfHoldingInventory.getInventoryFromStack(stack);
inventory.addItem((ItemEntity)itemsAround.get(0));
inventory.writeTostack(stack);
inventory.onInvClose(player);
inventory.addItem((ItemEntity)itemsAround.get(0));
inventory.writeTostack(stack);
inventory.onInvClose(player);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
} else if (hit.getType() == HitResult.Type.ENTITY) {
Entity e = ((EntityHitResult)hit).getEntity();
if (e instanceof LivingEntity && !(e instanceof PlayerEntity) && !(e instanceof IMagicals)) {
if (!world.isClient) {
BagOfHoldingInventory inventory = BagOfHoldingInventory.getInventoryFromStack(stack);
inventory.addEntity(world, e);
inventory.writeTostack(stack);
inventory.onInvClose(player);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
}
@ -108,9 +125,16 @@ public class BagOfHoldingItem extends Item implements MagicalItem {
return new TypedActionResult<>(ActionResult.FAIL, stack);
}
ContainerProviderRegistry.INSTANCE.openContainer(UContainers.BAG_OF_HOLDING, player, o -> {});
player.openContainer(new ContainerProvider(stack));
if (player instanceof ServerPlayerEntity) {
ContainerProviderRegistry.INSTANCE.openContainer(UContainers.BAG_OF_HOLDING, player, o -> {
if (stack.hasCustomName()) {
o.writeText(stack.getName());
} else {
o.writeText(new TranslatableText("unicopi.gui.title.itemofholding"));
}
});
// player.openContainer(new ContainerProvider(stack));
}
player.playSound(SoundEvents.BLOCK_ENDER_CHEST_OPEN, 0.5F, 1);
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
@ -120,28 +144,4 @@ public class BagOfHoldingItem extends Item implements MagicalItem {
public Affinity getAffinity() {
return Affinity.NEUTRAL;
}
public static class ContainerProvider implements NameableContainerFactory {
private Text customname = null;
ContainerProvider(ItemStack stack) {
if (stack.hasCustomName()) {
customname = stack.getName();
}
}
@Override
public Text getDisplayName() {
if (customname != null) {
return customname;
}
return new TranslatableText("unicopi.gui.title.itemofholding");
}
@Override
public BagOfHoldingContainer createMenu(int id, PlayerInventory inv, PlayerEntity player) {
return new BagOfHoldingContainer(id, null, player, null);
}
}
}