Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDyna committed Dec 17, 2024
1 parent bb69218 commit 7216b9c
Show file tree
Hide file tree
Showing 30 changed files with 509 additions and 211 deletions.
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ loader_version_range=[4,)

# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63}
# Must match the String constant located in the main mod class annotated with @Mod.
mod_id=examplemod
mod_id=btw_ores
# The human-readable display name for the mod.
mod_name=Example Mod
mod_name=Better That Wolfes - Ores
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved
mod_license=MIT
# The mod version. See https://semver.org/
mod_version=1.0.0
mod_version=1.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
mod_group_id=com.example.examplemod
mod_group_id=com.devdyna.btw_ores
# The authors of the mod. This is a simple text string that is used for display purposes in the mod list.
mod_authors=YourNameHere, OtherNameHere
mod_authors=DevDyna
# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list.
mod_description=Example mod description.\nNewline characters can be used and will be replaced properly.
mod_description=TODO
22 changes: 22 additions & 0 deletions src/main/java/com/devdyna/btw_ores/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devdyna.btw_ores;

import com.devdyna.btw_ores.events.BlockBreak;
import com.devdyna.btw_ores.registry.ItemsBlocks;
import com.devdyna.btw_ores.registry.Tab;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;

@Mod(Main.MODID)
public class Main {

public static final String MODID = "btw_ores";

public Main(IEventBus modEventBus, ModContainer modContainer) {
Tab.register(modEventBus);
ItemsBlocks.register(modEventBus);
NeoForge.EVENT_BUS.register(new BlockBreak());
}

}
95 changes: 95 additions & 0 deletions src/main/java/com/devdyna/btw_ores/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.devdyna.btw_ores;

import java.util.List;
import java.util.Random;



// import org.slf4j.Logger;

// import com.mojang.logging.LogUtils;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.neoforged.neoforge.registries.DeferredBlock;

public class Utils {

/**
* @param level net.minecraft.world.level.Level
* @param enchant Enchantments.name
* @return Holder[Enchantment]
*/
public static Holder<Enchantment> getEnchantHolder(LevelAccessor level, ResourceKey<Enchantment> enchant) {
return level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT)
.getOrThrow(enchant);
}

// private static final Logger LOGGER = LogUtils.getLogger();

@SuppressWarnings("null")
public static List<ItemStack> getItemStackFromLootTable(LevelAccessor level, Player player, String raw_ore_name) {

LootParams.Builder builder = new LootParams.Builder((ServerLevel) level);
LootParams params = builder.create(LootContextParamSets.EMPTY);
builder.withLuck(player.getLuck());

LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(ResourceKey
.create(Registries.LOOT_TABLE, ResourceLocation.fromNamespaceAndPath(
getModName(raw_ore_name), "blocks/"
+ raw_ore_name.substring(raw_ore_name.lastIndexOf('.') + 1))));
return lootTable.getRandomItems(params);

}

@SuppressWarnings({ "resource", "null" })
public void messageActionBar(String name) {
Minecraft.getInstance().player.displayClientMessage(Component.literal(name),
true);
}

public static int getRandomValue(int value) {
if (value == 0) {
return 1;
}

Random random = new Random();
return random.nextInt(value) + 1;
}

public static String getModName(String traslationName) {
String[] parts = traslationName.split("\\.");
if (parts.length >= 2) {
return parts[1];
} else {
return null;
}
}

public static boolean isDimension(Player player ,ResourceKey<Level> dim
){
return player.level().dimension().equals(dim);
}

public static void SimplePlaceBlock(LevelAccessor levelAccessor,BlockPos pos,DeferredBlock<Block> block){
levelAccessor.setBlock(pos, block.get().defaultBlockState(), 32);
}



}
92 changes: 92 additions & 0 deletions src/main/java/com/devdyna/btw_ores/events/BlockBreak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.devdyna.btw_ores.events;

import java.util.List;

import com.devdyna.btw_ores.Utils;
import com.devdyna.btw_ores.registry.ItemsBlocks;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.event.level.BlockEvent;
import net.neoforged.neoforge.event.level.BlockEvent.BreakEvent;
import net.neoforged.neoforge.registries.DeferredBlock;

public class BlockBreak {

@SubscribeEvent
public void BlockBreakEvent(BlockEvent.BreakEvent event) {

LevelAccessor levelAccessor = event.getLevel();
BlockPos pos = event.getPos();
Player player = event.getPlayer();
BlockState state = event.getState();
String raw_ore_name = state.getBlock().getDescriptionId();

if (state.is(Tags.Blocks.ORES) && !state.is(com.devdyna.btw_ores.registry.AnyTags.BLACKLISTED_ORES)
&& player.getMainHandItem()
.getEnchantmentLevel(Utils.getEnchantHolder(levelAccessor, Enchantments.SILK_TOUCH)) == 0) {

// ore broken on overworld with stone tag over 0
if (Utils.isDimension(player, Level.OVERWORLD) && state.is(Tags.Blocks.ORES_IN_GROUND_STONE)
&& pos.getY() >= 0) {
Utils.SimplePlaceBlock(levelAccessor, pos, ItemsBlocks.STONE_CLUSTER_BLOCK);
DropItems(event, raw_ore_name, ItemsBlocks.STONE_CLUSTER_BLOCK);

}
// ore broken on overworld with deepslate tag below 0
if (Utils.isDimension(player, Level.OVERWORLD) && state.is(Tags.Blocks.ORES_IN_GROUND_DEEPSLATE)
&& pos.getY() < 0) {
DropItems(event, raw_ore_name, ItemsBlocks.DEEP_CLUSTER_BLOCK);

}
// ore broken on nether with nether tag
if (Utils.isDimension(player, Level.NETHER) && state.is(Tags.Blocks.ORES_IN_GROUND_NETHERRACK)) {
DropItems(event, raw_ore_name, ItemsBlocks.NETHER_CLUSTER_BLOCK);
}
// ore broken on end with custom end tag with "end" without "nether" & "stone" &
// "deepslate"
if (Utils.isDimension(player, Level.END)
&& state.is(com.devdyna.btw_ores.registry.AnyTags.ORES_IN_GROUND_END)
&& raw_ore_name.contains("end")
&& !raw_ore_name.contains("nether") && !raw_ore_name.contains("deepslate")
&& !raw_ore_name.contains("stone")) {
DropItems(event, raw_ore_name, ItemsBlocks.END_CLUSTER_BLOCK);
}
// ore broken on other dimension
if (!Utils.isDimension(player, Level.OVERWORLD) && !Utils.isDimension(player, Level.NETHER)
&& !Utils.isDimension(player, Level.END)) {
DropItems(event, raw_ore_name, ItemsBlocks.NULL_CLUSTER_BLOCK);
}
}

}

private void DropItems(BreakEvent event, String raw_ore_name, DeferredBlock<Block> block) {
for (int i = 0; i < Utils.getRandomValue(event.getPlayer().getMainHandItem()
.getEnchantmentLevel(Utils.getEnchantHolder(event.getLevel(), Enchantments.FORTUNE))); i++) {

List<ItemStack> list = Utils.getItemStackFromLootTable(event.getLevel(), event.getPlayer(), raw_ore_name);

for (ItemStack itemStack : list) {
ItemEntity itementity = new ItemEntity((net.minecraft.world.level.Level) event.getLevel(),
event.getPos().getX(),
event.getPos().getY(),
event.getPos().getZ(),
itemStack);
event.getLevel().addFreshEntity(itementity);
}
event.setCanceled(true);
}
Utils.SimplePlaceBlock(event.getLevel(), event.getPos(), block);
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/devdyna/btw_ores/registry/AnyTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devdyna.btw_ores.registry;

import com.devdyna.btw_ores.Main;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;

public class AnyTags {

AnyTags() {
}


public static final TagKey<Block> BLACKLISTED_ORES = TagKey.create(BuiltInRegistries.BLOCK.key(),
ResourceLocation.fromNamespaceAndPath(Main.MODID, "cannot_spawn_ore_cluster"));

public static final TagKey<Block> ORES_IN_GROUND_END = TagKey.create(BuiltInRegistries.BLOCK.key(),
ResourceLocation.fromNamespaceAndPath("c", "ores_in_ground/end"));

}
49 changes: 49 additions & 0 deletions src/main/java/com/devdyna/btw_ores/registry/ItemsBlocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.devdyna.btw_ores.registry;

import com.devdyna.btw_ores.Main;

import net.minecraft.world.item.BlockItem;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredBlock;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;

public class ItemsBlocks {

ItemsBlocks() {
}

public static void register(IEventBus bus) {
ITEMS.register(bus);
BLOCKS.register(bus);
}

public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Main.MODID);
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Main.MODID);

public static final DeferredBlock<Block> STONE_CLUSTER_BLOCK = ClusterBlock("stone");
public static final DeferredBlock<Block> DEEP_CLUSTER_BLOCK = ClusterBlock("deep");
public static final DeferredBlock<Block> NETHER_CLUSTER_BLOCK = ClusterBlock("nether");
public static final DeferredBlock<Block> END_CLUSTER_BLOCK = ClusterBlock("end");
public static final DeferredBlock<Block> NULL_CLUSTER_BLOCK = ClusterBlock("null");



public static final DeferredItem<BlockItem> STONE_CLUSTER_ITEM = ClusterItem(STONE_CLUSTER_BLOCK);
public static final DeferredItem<BlockItem> DEEP_CLUSTER_ITEM = ClusterItem( DEEP_CLUSTER_BLOCK);
public static final DeferredItem<BlockItem> NETHER_CLUSTER_ITEM = ClusterItem( NETHER_CLUSTER_BLOCK);
public static final DeferredItem<BlockItem> END_CLUSTER_ITEM = ClusterItem( END_CLUSTER_BLOCK);
public static final DeferredItem<BlockItem> NULL_CLUSTER_ITEM = ClusterItem( NULL_CLUSTER_BLOCK);

public static DeferredBlock<Block> ClusterBlock(String name) {
return BLOCKS.registerSimpleBlock( name+"_cluster",
BlockBehaviour.Properties.of().destroyTime(10).explosionResistance(100));
}


public static DeferredItem<BlockItem> ClusterItem(DeferredBlock<Block> block ) {
return ITEMS.registerSimpleBlockItem(block);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/devdyna/btw_ores/registry/Tab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.devdyna.btw_ores.registry;

import com.devdyna.btw_ores.Main;

import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

public class Tab {

Tab() {
}

public static void register(IEventBus bus) {
CREATIVE_MODE_TABS.register(bus);
}

public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister
.create(Registries.CREATIVE_MODE_TAB, Main.MODID);
public static final DeferredHolder<CreativeModeTab, CreativeModeTab> CLUSTER_TAB = CREATIVE_MODE_TABS
.register("cluster_tab", () -> CreativeModeTab.builder()
.title(Component.translatable(Main.MODID + ".cluster.tab"))
.withTabsBefore(CreativeModeTabs.COMBAT)
.icon(() -> ItemsBlocks.STONE_CLUSTER_ITEM.get().getDefaultInstance())
.displayItems((parameters, output) -> {
output.accept(ItemsBlocks.STONE_CLUSTER_BLOCK.get());
output.accept(ItemsBlocks.DEEP_CLUSTER_BLOCK.get());
output.accept(ItemsBlocks.NETHER_CLUSTER_BLOCK.get());
output.accept(ItemsBlocks.END_CLUSTER_BLOCK.get());
output.accept(ItemsBlocks.NULL_CLUSTER_BLOCK.get());
}).build());
}
Loading

0 comments on commit 7216b9c

Please sign in to comment.