code cleanup

This commit is contained in:
Sen 2025-05-24 21:24:13 +02:00
parent d6d934f1f3
commit aa0ff6cf96
26 changed files with 207 additions and 276 deletions

View file

@ -1,22 +1,16 @@
package common.ai;
import java.util.function.Predicate;
import common.block.BlockTallGrass;
import common.entity.animal.EntitySheep;
import common.init.BlockRegistry;
import common.init.Blocks;
import common.init.Config;
import common.pattern.BlockStateHelper;
import common.util.BlockPos;
import common.util.Predicates;
import common.world.State;
import common.world.World;
public class EntityAIEatGrass extends EntityAIBase
{
private static final Predicate<State> field_179505_b = BlockStateHelper.forBlock(Blocks.tallgrass).where(BlockTallGrass.TYPE, Predicates.equalTo(BlockTallGrass.EnumType.GRASS));
private EntitySheep grassEaterEntity;
private World entityWorld;
int eatingGrassTimer;
@ -40,7 +34,9 @@ public class EntityAIEatGrass extends EntityAIBase
else
{
BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);
return field_179505_b.test(this.entityWorld.getState(blockpos)) ? true : this.entityWorld.getState(blockpos.down()).getBlock() == Blocks.grass;
State state = this.entityWorld.getState(blockpos);
return (state.getBlock() == Blocks.tallgrass && state.getValue(BlockTallGrass.TYPE) == BlockTallGrass.EnumType.GRASS) ||
this.entityWorld.getState(blockpos.down()).getBlock() == Blocks.grass;
}
}
@ -89,14 +85,15 @@ public class EntityAIEatGrass extends EntityAIBase
{
BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);
if (field_179505_b.test(this.entityWorld.getState(blockpos)))
State state = this.entityWorld.getState(blockpos);
if (state.getBlock() == Blocks.tallgrass && state.getValue(BlockTallGrass.TYPE) == BlockTallGrass.EnumType.GRASS)
{
if (Config.mobGrief)
{
this.entityWorld.destroyBlock(blockpos, false);
}
this.grassEaterEntity.eatGrassBonus();
this.grassEaterEntity.eatGrass();
}
else
{
@ -110,7 +107,7 @@ public class EntityAIEatGrass extends EntityAIBase
this.entityWorld.setState(blockpos1, Blocks.dirt.getState(), 2);
}
this.grassEaterEntity.eatGrassBonus();
this.grassEaterEntity.eatGrass();
}
}
}

View file

@ -330,7 +330,7 @@ public class EntitySheep extends EntityAnimal
* This function applies the benefits of growing back wool and faster growing up to the acting entity. (This
* function is used in the AIEatGrass)
*/
public void eatGrassBonus()
public void eatGrass()
{
this.setSheared(false);

View file

@ -1,61 +0,0 @@
package common.pattern;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;
import common.block.Block;
import common.collect.Maps;
import common.properties.IProperty;
import common.world.State;
public class BlockStateHelper implements Predicate<State>
{
private final Block block;
private final Map<IProperty, Predicate> predicates = Maps.<IProperty, Predicate>newHashMap();
private BlockStateHelper(Block blockStateIn)
{
this.block = blockStateIn;
}
public static BlockStateHelper forBlock(Block blockIn)
{
return new BlockStateHelper(blockIn);
}
public boolean test(State p_apply_1_)
{
if (p_apply_1_ != null && p_apply_1_.getBlock().equals(this.block))
{
for (Entry<IProperty, Predicate> entry : this.predicates.entrySet())
{
Object object = p_apply_1_.getValue((IProperty)entry.getKey());
if (!((Predicate)entry.getValue()).test(object))
{
return false;
}
}
return true;
}
else
{
return false;
}
}
public <V extends Comparable<V>> BlockStateHelper where(IProperty<V> property, Predicate <? extends V > is)
{
if (!this.block.getPropertyMap().contains(property))
{
throw new IllegalArgumentException(this.block + " cannot support property " + property);
}
else
{
this.predicates.put(property, is);
return this;
}
}
}

View file

@ -1,97 +0,0 @@
package common.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import common.log.Log;
import io.netty.util.CharsetUtil;
public class FileUtils {
public static String read(InputStream input) throws IOException {
return new String(readBytes(input), CharsetUtil.UTF_8);
}
public static byte[] readBytes(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
long count = 0L;
int n;
for(boolean u = false; -1 != (n = input.read(buffer)); count += (long)n) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
public static String read(File file) throws IOException {
FileInputStream in = null;
String s;
try {
in = new FileInputStream(file);
s = FileUtils.read(in);
}
finally {
try {
if(in != null)
in.close();
}
catch(IOException e) {
}
}
return s;
}
public static void write(File file, String data) throws IOException {
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
if(out != null) {
out.write(data.getBytes(CharsetUtil.UTF_8));
out.close();
}
}
finally {
try {
if(out != null)
out.close();
}
catch(IOException e) {
}
}
}
public static InputStream getResource(String path) throws FileNotFoundException {
InputStream in = FileUtils.class.getResourceAsStream("/" + path);
if(in == null)
throw new FileNotFoundException(path);
return in;
}
public static boolean deleteFiles(File[] files) {
if(files == null) {
Log.JNI.warn("Konnte Ordner nicht löschen");
return false;
}
for(int i = 0; i < files.length; ++i) {
File file = files[i];
Log.JNI.info("Lösche " + file);
if(file.isDirectory() && !deleteFiles(file.listFiles())) {
Log.JNI.warn("Konnte Ordner " + file + " nicht löschen");
return false;
}
if(!file.delete()) {
Log.JNI.warn("Konnte Datei " + file + " nicht löschen");
return false;
}
}
return true;
}
}