Skip to content

Commit

Permalink
Fixed weird NPE when gathering annotation data, added support for JEI…
Browse files Browse the repository at this point in the history
… extended GUI areas & made waterlogging easier to implement
  • Loading branch information
Elec332 committed Jan 9, 2020
1 parent b9443a3 commit 5f8749e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 14 deletions.
46 changes: 46 additions & 0 deletions src/main/java/elec332/core/block/AbstractBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

import com.google.common.base.Preconditions;
import elec332.core.tile.ITileWithDrops;
import elec332.core.util.BlockProperties;
import elec332.core.world.WorldHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.IWaterLoggable;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluids;
import net.minecraft.fluid.IFluidState;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootParameters;
Expand All @@ -33,6 +40,9 @@ public abstract class AbstractBlock extends Block implements IAbstractBlock {

public AbstractBlock(Properties builder) {
super(builder);
if (getDefaultState().has(BlockProperties.WATERLOGGED)) {
setDefaultState(getDefaultState().with(BlockStateProperties.WATERLOGGED, false));
}
}

private String unlocalizedName;
Expand Down Expand Up @@ -122,6 +132,42 @@ public List<ItemStack> getDrops(List<ItemStack> drops, @Nonnull LootContext.Buil
return drops;
}

@Nonnull
@Override
@SuppressWarnings("deprecation")
public BlockState updatePostPlacement(@Nonnull BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
if (stateIn.has(BlockProperties.WATERLOGGED) && stateIn.get(BlockStateProperties.WATERLOGGED)) {
worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn));
}
return super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
}

@Nonnull
@Override
@SuppressWarnings("deprecation")
public IFluidState getFluidState(BlockState state) {
if (state.has(BlockProperties.WATERLOGGED) && state.get(BlockStateProperties.WATERLOGGED)) {
return Fluids.WATER.getStillFluidState(false);
}
return super.getFluidState(state);
}

@Override
public boolean propagatesSkylightDown(BlockState state, @Nonnull IBlockReader reader, @Nonnull BlockPos pos) {
if (state.has(BlockProperties.WATERLOGGED) && state.get(BlockStateProperties.WATERLOGGED)) {
return false;
}
return super.propagatesSkylightDown(state, reader, pos);
}

@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
super.fillStateContainer(builder);
if (this instanceof IWaterLoggable) {
builder.add(BlockStateProperties.WATERLOGGED);
}
}

@SuppressWarnings("all")
public final List<ItemStack> getOriginalDrops(BlockState state, LootContext.Builder builder) {
return super.getDrops(state, builder);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/elec332/core/compat/jei/JeiCompat.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package elec332.core.compat.jei;

import elec332.core.ElecCore;
import elec332.core.inventory.window.WindowGui;
import elec332.core.util.RegistryHelper;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
import mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.ISubtypeRegistration;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import javax.annotation.Nonnull;
import java.util.List;

/**
* Created by Elec332 on 5-1-2020
Expand Down Expand Up @@ -41,4 +46,17 @@ public String apply(@Nonnull ItemStack itemStack) {
}
}

@Override
public void registerGuiHandlers(IGuiHandlerRegistration registration) {
registration.addGuiContainerHandler(WindowGui.class, new IGuiContainerHandler<WindowGui>() {

@Nonnull
@Override
public List<Rectangle2d> getGuiExtraAreas(WindowGui window) {
return window.getGuiExtraAreas();
}

});
}

}
22 changes: 21 additions & 1 deletion src/main/java/elec332/core/inventory/widget/slot/WidgetSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,44 @@ public WidgetSlot(IItemHandler inventory, int index, int x, int y) {
this.inventory = inventory;
this.slotIndex = index;
this.changeListeners = Lists.newArrayList();
this.ghostBackground = this.skipBackground = false;
}

private final List<Consumer<WidgetSlot>> changeListeners;
private final IItemHandler inventory;
private final int slotIndex;
private boolean ghostBackground, skipBackground;

public WidgetSlot addChangeListener(Consumer<WidgetSlot> listener) {
changeListeners.add(listener);
return this;
}

public WidgetSlot setGhostBackground() {
ghostBackground = true;
return this;
}

public WidgetSlot setSkipBackground() {
skipBackground = true;
return this;
}

protected void notifyChangeListeners() {
changeListeners.forEach(l -> l.accept(this));
}

@Override
public void draw(Window window, int guiX, int guiY, double mouseX, double mouseY, float partialTicks) {
drawHollow(guiX, guiY, -1, -1, 18, 18);
if (skipBackground) {
return;
}
if (ghostBackground) {
bindTexture(Window.DEFAULT_BACKGROUND);
GuiDraw.drawTexturedModalRect(guiX + x - 1, guiY + y - 1, 198, 0, 18, 18);
} else {
drawHollow(guiX, guiY, -1, -1, 18, 18);
}
}

@Override
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/elec332/core/inventory/window/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import elec332.core.inventory.widget.slot.WidgetSlot;
import elec332.core.inventory.widget.slot.WidgetSlotOutput;
import elec332.core.util.ItemStackHelper;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.container.ClickType;
Expand Down Expand Up @@ -352,6 +353,20 @@ protected void handleSlotClick(@Nullable WidgetSlot slotIn, int slotId, int mous
windowContainer.handleSlotClickDefault(slotIn, slotId, mouseButton, type);
}

/**
* Used for JEI, makes sure the GUI doesn't interfere with JEI's stuff
*
* @param extraAreas A list that extra used areas can be added to
*/
@OnlyIn(Dist.CLIENT)
public void addExtraGuiAreas(List<Rectangle2d> extraAreas) {
}

@OnlyIn(Dist.CLIENT)
protected Rectangle2d createRelativeRectangle(int x, int y, int width, int height) {
return new Rectangle2d(guiLeft + x, guiTop + y, width, height);
}

@Override
@OnlyIn(Dist.CLIENT)
public void mouseMoved(double mouseX, double mouseY) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/elec332/core/inventory/window/WindowGui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elec332.core.inventory.window;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.platform.GlStateManager;
import elec332.core.ElecCore;
import elec332.core.util.InventoryHelper;
Expand All @@ -9,6 +10,7 @@
import net.minecraft.client.MouseHelper;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Slot;
Expand Down Expand Up @@ -70,6 +72,12 @@ public boolean isPauseScreen() {
return window.doesWindowPauseGame();
}

public List<Rectangle2d> getGuiExtraAreas() {
List<Rectangle2d> ret = Lists.newArrayList();
window.addExtraGuiAreas(ret);
return ret;
}

@Override
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, @Nonnull ClickType type) {
window.handleSlotClick(slotIn == null ? null : ((WindowContainer.WidgetLinkedSlot) slotIn).widget, slotId, mouseButton, type);
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/elec332/core/loader/AnnotationDataHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package elec332.core.loader;

import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import elec332.core.api.APIHandlerInject;
import elec332.core.api.IAPIHandler;
Expand Down Expand Up @@ -106,18 +107,20 @@ void identify(ModList modList) {
.collect(Collectors.toMap(Function.identity(), mfi -> {
ModFile mf = mfi.getFile();
SetMultimap<Type, IAnnotationData> ret = HashMultimap.create();
mf.getScanResult().getAnnotations().forEach(ad -> {
IAnnotationData annotationData = new AnnotationData(ad, mf);
if (annotationData.getAnnotationName().startsWith("Ljava/lang") || annotationData.getAnnotationName().startsWith("Ljavax/annotation")) {
return;
}
Type annType = ad.getAnnotationType();
ret.put(annType, annotationData);
ModContainer mc = modSearcher.apply(annotationData);
if (mc != null) {
annotationDataM.computeIfAbsent(mc.getModId(), s -> HashMultimap.create()).put(annType, annotationData);
}
});
mf.getScanResult().getAnnotations().stream()
.filter(Objects::nonNull)
.forEach(ad -> {
IAnnotationData annotationData = new AnnotationData(ad, mf);
if (annotationData.getAnnotationName().startsWith("Ljava/lang") || annotationData.getAnnotationName().startsWith("Ljavax/annotation")) {
return;
}
Type annType = ad.getAnnotationType();
ret.put(annType, annotationData);
ModContainer mc = modSearcher.apply(annotationData);
if (mc != null) {
annotationDataM.computeIfAbsent(mc.getModId(), s -> HashMultimap.create()).put(annType, annotationData);
}
});
return ret;
}));
final SetMultimap<Type, IAnnotationData> annotationData = HashMultimap.create();
Expand Down Expand Up @@ -248,7 +251,7 @@ public void injectASMHelper(IAPIHandler apiHandler) {
private static class AnnotationData implements IAnnotationData {

private AnnotationData(ModFileScanData.AnnotationData asmData, ModFile file) {
this.asmData = asmData;
this.asmData = Preconditions.checkNotNull(asmData);
this.modFile = file;
this.isField = asmData.getMemberName().indexOf('(') == -1;
this.isClass = asmData.getMemberName().indexOf('.') != -1;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/elec332/core/util/BlockProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package elec332.core.util;

import net.minecraft.state.BooleanProperty;
import net.minecraft.state.EnumProperty;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
Expand All @@ -10,5 +11,6 @@
public final class BlockProperties {

public static final EnumProperty<Direction> FACING_NORMAL = BlockStateProperties.FACING;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;

}

0 comments on commit 5f8749e

Please sign in to comment.