Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/dev/jaronline/simplyhopper/SimplyHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.mojang.logging.LogUtils;
import dev.jaronline.simplyhopper.block.BlockRegistry;
import dev.jaronline.simplyhopper.block.entity.BlockEntityRegistry;
import dev.jaronline.simplyhopper.entity.EntityRegistry;
import dev.jaronline.simplyhopper.gui.CreativeTabRegistry;
import dev.jaronline.simplyhopper.gui.MenuTypeRegistry;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.entity.MinecartRenderer;
import dev.jaronline.simplyhopper.item.ItemRegistry;
import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartingEvent;
Expand All @@ -29,6 +32,7 @@ public SimplyHopper() {

BlockRegistry.register(modEventBus);
BlockEntityRegistry.register(modEventBus);
EntityRegistry.register(modEventBus);
ItemRegistry.register(modEventBus);
CreativeTabRegistry.register(modEventBus);
// MenuTypeRegistry.register(modEventBus);
Expand All @@ -50,6 +54,12 @@ public static class ClientModEvents {
@SubscribeEvent
public static void onClientSetup(final FMLClientSetupEvent event) {
LOGGER.info("Client setup");
event.enqueueWork(()->{
EntityRenderers.register(
EntityRegistry.SIMPLY_HOPPER_MINECART.getEntity(),
context -> new MinecartRenderer<>(context, ModelLayers.MINECART)
);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.jaronline.simplyhopper.entity;

import dev.jaronline.simplyhopper.SimplyHopper;
import dev.jaronline.simplyhopper.item.ItemRegistry;
import dev.jaronline.simplyhopper.item.SimplyHopperMinecartItem;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.projectile.ItemSupplier;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.MinecartItem;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Supplier;

public class EntityRegistry {
private static final DeferredRegister<EntityType<?>> ENTITIES = DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, SimplyHopper.MOD_ID);

public static final RegistryEntityItem<EntityType<SimplyHopperMinecartEntity>,SimplyHopperMinecartItem> SIMPLY_HOPPER_MINECART = registerMinecartEntity(
"simply_hopper_minecart",
() -> EntityType.Builder.<SimplyHopperMinecartEntity>of(
SimplyHopperMinecartEntity::new, MobCategory.MISC)
.sized(0.98F, 0.7F)
.clientTrackingRange(8)
.build(SimplyHopper.MOD_ID + ":simply_hopper_minecart"),
() -> new SimplyHopperMinecartItem(new Item.Properties()));


public static <T extends Entity,R extends MinecartItem> RegistryEntityItem<EntityType<T>,R> registerMinecartEntity(String name, Supplier<EntityType<T>> entityTypeSupplier, Supplier<R> itemSupplier) {
RegistryObject<EntityType<T>> registryEntity = ENTITIES.register(name, entityTypeSupplier);
RegistryObject<R> item = ItemRegistry.registerMinecartItem(name, itemSupplier);
return new RegistryEntityItem<>(registryEntity, item);
}

public static void register(IEventBus eventBus) {
ENTITIES.register(eventBus);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.jaronline.simplyhopper.entity;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.registries.RegistryObject;

public class RegistryEntityItem <T extends EntityType<? extends Entity>, R extends Item> {
private final RegistryObject<T> entity;
private final RegistryObject<R> item;

RegistryEntityItem(RegistryObject<T> entity, RegistryObject<R> item) {
this.entity = entity;
this.item = item;
}

public RegistryObject<T> getRegistryEntity() {
return entity;
}

public T getEntity() {
return entity.get();
}

public RegistryObject<R> getRegistryEntityItem() {
return item;
}

public R getItem() {
return item.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package dev.jaronline.simplyhopper.entity;

//import net.minecraft.world.entity.EntityType;
import dev.jaronline.simplyhopper.block.BlockRegistry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.vehicle.*;
//import net.minecraft.world.entity.vehicle.MinecartHopper;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.HopperMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.Hopper;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class SimplyHopperMinecartEntity extends AbstractMinecartContainer implements Hopper {
private boolean enabled = true;

public SimplyHopperMinecartEntity(EntityType<? extends SimplyHopperMinecartEntity> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
}

public SimplyHopperMinecartEntity(Level pLevel, double pX, double pY, double pZ) {
super(EntityRegistry.SIMPLY_HOPPER_MINECART.getEntity(), pX, pY, pZ, pLevel);
}

public AbstractMinecart.@NotNull Type getMinecartType() {
return AbstractMinecart.Type.HOPPER;
}

public @NotNull BlockState getDefaultDisplayBlockState() {
return BlockRegistry.SIMPLE_HOPPER_BLOCK.getBlock().defaultBlockState();
}

public int getDefaultDisplayOffset() {
return 1;
}

/**
* Returns the number of slots in the inventory.
*/
public int getContainerSize() {
return 5;
}

/**
* Called every tick the minecart is on an activator rail.
*/
public void activateMinecart(int pX, int pY, int pZ, boolean pReceivingPower) {
boolean flag = !pReceivingPower;
if (flag != this.isEnabled()) {
this.setEnabled(flag);
}

}

/**
* Get whether this hopper minecart is being blocked by an activator rail.
*/
public boolean isEnabled() {
return this.enabled;
}

/**
* Set whether this hopper minecart is being blocked by an activator rail.
*/
public void setEnabled(boolean pEnabled) {
this.enabled = pEnabled;
}

/**
* Gets the world X position for this hopper entity.
*/
public double getLevelX() {
return this.getX();
}

/**
* Gets the world Y position for this hopper entity.
*/
public double getLevelY() {
return this.getY() + 0.5D;
}

/**
* Gets the world Z position for this hopper entity.
*/
public double getLevelZ() {
return this.getZ();
}

/**
* Called to update the entity's position/logic.
*/
public void tick() {
super.tick();
if (!this.level().isClientSide && this.isAlive() && this.isEnabled() && this.suckInItems()) {
this.setChanged();
}

}

// TODO: Fix that suckInItems uses same logic as SimplyHopperBlockEntity
public boolean suckInItems() {
// return false;
if (HopperBlockEntity.suckInItems(this.level(), this)) {
return true;
} else {
for(ItemEntity itementity : this.level().getEntitiesOfClass(ItemEntity.class, this.getBoundingBox().inflate(0.25D, 0.0D, 0.25D), EntitySelector.ENTITY_STILL_ALIVE)) {
if (HopperBlockEntity.addItem(this, itementity)) {
return true;
}
}

return false;
}
}

protected @NotNull Item getDropItem() {
return EntityRegistry.SIMPLY_HOPPER_MINECART.getItem();
}

protected void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putBoolean("Enabled", this.enabled);
}

/**
* (abstract) Protected helper method to read subclass entity data from NBT.
*/
protected void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
this.enabled = pCompound.contains("Enabled") ? pCompound.getBoolean("Enabled") : true;
}

public @NotNull AbstractContainerMenu createMenu(int pId, @NotNull Inventory pPlayerInventory) {
return new HopperMenu(pId, pPlayerInventory, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.jaronline.simplyhopper.gui;

import dev.jaronline.simplyhopper.block.BlockRegistry;
import dev.jaronline.simplyhopper.entity.EntityRegistry;
import dev.jaronline.simplyhopper.item.ItemRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
Expand All @@ -16,11 +19,12 @@ public class CreativeTabRegistry {

static {
CREATIVE_MODE_TABS.register(
"simply_hopper_tab", () -> CreativeModeTab.builder()
"simply_hopper_tab", () -> CreativeModeTab.builder()
.title(Component.translatable("itemGroup.simply_hopper"))
.icon(() -> BlockRegistry.SIMPLY_HOPPER_BLOCK.getBlockItem().getDefaultInstance())
.displayItems((parameters, output) -> {
output.accept(BlockRegistry.SIMPLY_HOPPER_BLOCK.getBlockItem());
output.accept(EntityRegistry.SIMPLY_HOPPER_MINECART.getItem());
})
.build());
}
Expand All @@ -31,7 +35,9 @@ public static void register(IEventBus eventBus) {
}

private static void addCreative(BuildCreativeModeTabContentsEvent event) {
if (event.getTabKey() == CreativeModeTabs.REDSTONE_BLOCKS)
if (event.getTabKey() == CreativeModeTabs.REDSTONE_BLOCKS) {
event.accept(BlockRegistry.SIMPLY_HOPPER_BLOCK.getBlockItem());
event.accept(EntityRegistry.SIMPLY_HOPPER_MINECART.getItem());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dev.jaronline.simplyhopper.item;

import dev.jaronline.simplyhopper.SimplyHopper;
import dev.jaronline.simplyhopper.entity.SimplyHopperBlockEntity;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.MinecartItem;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
Expand All @@ -20,4 +24,8 @@ public static void register(IEventBus eventBus) {
public static RegistryObject<BlockItem> registerBlockItem(String name, Supplier<BlockItem> blockItem) {
return ITEMS.register(name, blockItem);
}

public static <T extends MinecartItem> RegistryObject<T> registerMinecartItem(String name, Supplier<T> minecartItem) {
return ITEMS.register(name, minecartItem);
}
}
Loading